【问题标题】:How to run a java code from command prompt which is using API?如何从使用 API 的命令提示符运行 java 代码?
【发布时间】:2013-02-07 12:31:41
【问题描述】:

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

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

它编译成功,但是当我运行它时

$ java MyCode

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

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

【问题讨论】:

    标签: java shell command-line jar


    【解决方案1】:

    将 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
    

    【讨论】:

    • 我找到了答案。感谢你们对我的帮助。 $ javac -cp [path-to-twitter4j-jars] MyCode.java $ java -cp .:[path-to-twitter4j-jars/myjar.jar] MyCode
    【解决方案2】:

    在运行您的 Java 应用程序时,JVM 还需要了解 jar。

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

    这是link to java command-line options

    【讨论】:

      【解决方案3】:

      运行您的应用程序时,JAR 文件仍需要位于类路径中,而不仅仅是在编译时。

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多