如何从使用API​​的命令提示符运行java代码?

时间:2013-02-22 00:33:10

标签: java shell command-line jar

我编写的代码在jar文件中使用了Twitter4J API的类。我想通过命令提示符运行我的代码。我该怎么办?我试过了:

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

它已成功编译,但当我按

运行时
$ java MyCode

但它没有找到Twitter4J课程。我正在我的代码中创建该类的实例。

有人可以帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:3)

类路径是Java通过包搜索fodler中的实际.class文件的位置 Java不会查看类路径文件夹中的JAR。

您需要将JAR文件本身放在类路径中;不是包含它的文件夹。

(或者,您可以包含folder/*.jar将每个JAR放在类路径中的该文件夹中)

答案 1 :(得分:2)

运行你的应用程序时,JAR文件仍然需要在类路径上,而不仅仅是在编译时。

答案 2 :(得分:2)

运行Java应用程序时,JVM也需要了解jar文件。

java -cp [path-to-twitter4j-jars] MyCode

这是link to java command-line options

答案 3 :(得分:2)

将jar添加到classpath:

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