我为 Twitter4J API 编写了使用 jar 文件中的类的代码。我想通过命令提示符运行我的代码。我该怎么做?我试过了:
$ javac -cp [path-to-twitter4j-jars] MyCode.java
它已成功编译,但是当我运行它时
$ java MyCode
但它没有找到 Twitter4J 类。我正在我的代码中创建该类的一个实例。
有人可以帮我解决这个问题吗?
我为 Twitter4J API 编写了使用 jar 文件中的类的代码。我想通过命令提示符运行我的代码。我该怎么做?我试过了:
$ javac -cp [path-to-twitter4j-jars] MyCode.java
它已成功编译,但是当我运行它时
$ java MyCode
但它没有找到 Twitter4J 类。我正在我的代码中创建该类的一个实例。
有人可以帮我解决这个问题吗?
.class类路径是 Java按包在 fodler中搜索实际文件的地方。
Java 不会查看类路径文件夹中的 JAR。
您需要将 JAR 文件本身放在类路径中;不是包含它的文件夹。
(或者,您可以folder/*.jar将每个 JAR 放入类路径中的该文件夹中)
将 jars 添加到类路径:
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
    运行应用程序时,JAR 文件仍然需要位于类路径中,而不仅仅是编译时。