简体   繁体   English

用java -classpath写什么路径?

[英]What path to write in java -classpath?

I have a package with many files and directories (Californium, a CoAP protocol implementation). 我有一个包含许多文件和目录的软件包(加利福尼亚,一种CoAP协议实现)。 It comes as a Maven project. 它作为Maven项目来。 I have gone to the directory of the pom.xml file and ran mvn package in the terminal. 我已经转到pom.xml文件的目录,并在终端中运行了mvn package Everything went fine. 一切都很好。 But upon going to one of the examples (Helloworldserver.class), and trying to run it with java HelloWorldServer I get: 但是,转到其中一个示例(Helloworldserver.class),并尝试使用java HelloWorldServer运行它时,我得到:

Error: Could not find or load main class HelloWorldServer . Error: Could not find or load main class HelloWorldServer

I look at the classpath by typing echo $CLASSPATH and I get nothing. 我通过键入echo $CLASSPATH来查看类路径,但我什么也没得到。 So I try to set the classpath: 所以我尝试设置类路径:

java -classpath org.eclipse.californium.examples

Which generates a description of all the possible options available for the java command, and does NOT set the classpath. 它将生成可用于java命令的所有可能选项的描述,并且不设置类路径。

I chose the classpath in the above command because in the HelloWorldServer.java, there is the first line: 我在上面的命令中选择了类路径,因为在HelloWorldServer.java中,存在第一行:

package org.eclipse.californium.examples;

I suspect this is wrong, judging by the output of my command. 根据命令的输出,我怀疑这是错误的。 How is the correct way to specify the path? 如何指定路径的正确方法? What do I want to point to? 我想指出什么?

When you built the code using Maven, it will have created a "target" directory, and within that a "classes" directory tree that contains the class files produced by the compiler. 使用Maven构建代码时,它将创建一个“目标”目录,并在其中创建一个“类”目录树,其中包含编译器生成的类文件。

Find that "target/classes" directory or whatever it is called. 查找该“ target / classes”目录或任何其他目录。 (It is called "target/classes" in the standard Maven file structure, but the POM file could specify otherwise.) (在标准Maven文件结构中称为“目标/类”,但POM文件可以另外指定。)

Now run the application like this: 现在像这样运行应用程序:

  $ java -classpath target/classes:<dependency-1>:<dependency-2>:... \
        org.eclipse.californium.examples.HelloWorldServer

where <dependency-1> and <dependency-2> and so on are the pathnames for the dependency JAR files ... as downloaded into your local .m2 repo by Maven. 其中, <dependency-1><dependency-2>等是依赖关系JAR文件的路径名...由Maven下载到本地.m2库中。


The general pattern that the -classpath parameter is a colon-separated 1 list of directory names, jar/zip file names or jar/zip file patterns. -classpath参数是由冒号分隔的1个目录名称,jar / zip文件名或jar / zip文件模式的列表的常规模式。 If you run against classes that have not been packaged into a JAR or ZIP file, you need to give the root of the class tree. 如果针对尚未打包到JAR或ZIP文件中的类运行,则需要提供类树的根。 For instance, if the full name for your HelloWorldServer class is 例如,如果您的HelloWorldServer类的全名是

    org.eclipse.californium.examples.HelloWorldServer

then the target directory tree will look like this: 然后目标目录树将如下所示:

  ...
  target:
    classes:
      org:
        eclipse:
          californium:
            examples:
              HelloWorldServer.class

The "target/classes" is the correct root directory. “目标/类”是正确的根目录。 The java command will attempt to navigate to the "HelloWorldServer.class" file based on the package naming. java命令将尝试根据包命名导航到“ HelloWorldServer.class”文件。

Do not be tempted to do this: 不要试图这样做:

$ java -classpath target/classes/org/eclipse/californium/examples:... \
     HelloWorldServer.class

It won't work. 它不会工作。


1 - or a semicolon on Windows. 1-或Windows上的分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM