2013-02-22 43 views
1

我已經編寫了使用Twitter4J API的jar文件中的類的代碼。我想通過命令提示符運行我的代碼。我會怎麼做?我想:如何從使用API​​的命令提示符運行java代碼?

$ javac -cp [path-to-twitter4j-jars] MyCode.java 

它得到了編譯成功,但是當我通過

$ java MyCode 

運行它,但它沒有找到Twitter4J類。我在我的代碼中創建了該類的一個實例。

請問有誰能幫我解決這個問題?

回答

2

類路徑是Java根據包在fodlers中搜索實際.class文件的位置。
Java不查看位於類路徑文件夾內的JAR。

您需要將JAR文件本身放在classpath中;而不是包含它的文件夾。

(或者,您可以包括folder/*.jar把每一個JAR文件夾在類路徑)

1

的jar文件仍然需要在類路徑中運行時,你應用,編譯不只是當。

1

添加罐子類路徑:

java -cp .:path/to/twitter4j/jars MyCode 

文檔:

where options include: 
-d32  use a 32-bit data model if available 
-d64  use a 64-bit data model if available 
-server to select the "server" VM 
       The default VM is server, 
       because you are running on a server-class machine. 


-cp <class search path of directories and zip/jar files> 
-classpath <class search path of directories and zip/jar files> 
       A : separated list of directories, JAR archives, 
       and ZIP archives to search for class files. 
-D<name>=<value> 
       set a system property 
-verbose:[class|gc|jni] 
       enable verbose output 
-version  print product version and exit 
-version:<value> 
       require the specified version to run 
-showversion print product version and continue 
-jre-restrict-search | -no-jre-restrict-search 
       include/exclude user private JREs in the version search 
-? -help  print this help message 
-X   print help on non-standard options 
-ea[:<packagename>...|:<classname>] 
-enableassertions[:<packagename>...|:<classname>] 
       enable assertions with specified granularity 
-da[:<packagename>...|:<classname>] 
-disableassertions[:<packagename>...|:<classname>] 
       disable assertions with specified granularity 
-esa | -enablesystemassertions 
       enable system assertions 
-dsa | -disablesystemassertions 
       disable system assertions 
-agentlib:<libname>[=<options>] 
       load native agent library <libname>, e.g. -agentlib:hprof 
       see also, -agentlib:jdwp=help and -agentlib:hprof=help 
-agentpath:<pathname>[=<options>] 
       load native agent library by full pathname 
-javaagent:<jarpath>[=<options>] 
       load Java programming language agent, see java.lang.instrument 
-splash:<imagepath> 
       show splash screen with specified image 
+0

我找到了答案。感謝你們對我的幫助。 '$ javac -cp [path-to-twitter4j-jars] MyCode.java $ java -cp。:[path-to-twitter4j-jars/myjar.jar] MyCode' – sattu 2013-02-22 02:06:01

+0

yes將'.'加入類路徑以尋找'main ()' – lichengwu 2013-02-22 05:31:17

相關問題