简体   繁体   English

如何在命令行中为多个包设置类路径?

[英]how to set classpath for multiple packages in command line?

我正在尝试制作一个 java 程序来导入 2 个包,但不知道如何在单个命令行中为多个包设置类路径。

Just call, java -cp <CLASSPATH> <MainClass> .只需调用java -cp <CLASSPATH> <MainClass> Read here PATH and CLASSPATH to find the right syntax for the CLASSPATH according your operating system.阅读此处的PATH 和 CLASSPATH以根据您的操作系统找到CLASSPATH的正确语法。

You might want to read the Oracle tech note on setting the classpath , but the general idea is that you have a single classpath variable / command line argument that is composed of multiple paths separated using the standard path separator for your platform - on Windows this is generally ";"您可能想阅读有关设置类路径Oracle 技术说明,但总体思路是您有一个类路径变量/命令行参数,该参数由使用平台的标准路径分隔符分隔的多个路径组成 - 在 Windows 上,这是一般来说 ”;” and on Unix ":" (I say generally because if you use bash via cygwin or similar on Windows you will use the unix separator).在 Unix 上“:”(我说一般是因为如果您在 Windows 上通过 cygwin 或类似方式使用 bash,您将使用 unix 分隔符)。

Regardless, the classpath is made up of paths to directories that contain class files, to a specific jar file, or (in recent java versions) a wildcard that will match jars (but not classes).无论如何,类路径由包含类文件的目录、特定 jar 文件或(在最近的 Java 版本中)匹配 jar(但不是类)的通配符的路径组成。

If you have classes in a jar or directory, they must be in directory-package format, eg the following class is expected to be in a directory foo/bar/Baz.class :如果您在 jar 或目录中有类,则它们必须采用目录包格式,例如,以下类应位于目录foo/bar/Baz.class

package foo.bar;

public class Baz {
    public static void main(final String[] args) {
    }
}

So, given a Unix directory /home/user/project/classes (which contains a directory foo/bar containing Baz.class , a jar file /home/user/project/lib/dependency.jar , and a current working directory of /home/user/project we have:因此,给定一个 Unix 目录/home/user/project/classes (其中包含一个包含Baz.class的目录foo/bar 、一个 jar 文件/home/user/project/lib/dependency.jar以及/home/user/project我们有:

  • The classpath using absolute paths: /home/user/project/classes:/home/user/project/lib/dependency.jar .使用绝对路径的类路径: /home/user/project/classes:/home/user/project/lib/dependency.jar

  • The classpath using relative paths classes:lib/dependency.jar .使用相对路径classes:lib/dependency.jar的类路径。

One can pass this to java using either the -classpath or -cp command line arguments:可以使用-classpath-cp命令行参数将其传递给 java:

  • java -classpath classes:lib/dependency.jar foo.bar.Baz
  • java -cp classes:lib/dependency.jar foo.bar.Baz

Alternatively, the CLASSPATH environment variable can be set (it is usually more obvious to set it on the command line and less prone to causing unexpected side effects).或者,可以设置 CLASSPATH 环境变量(通常在命令行中设置更明显,不易引起意外副作用)。 In bash this might be:在 bash 中,这可能是:

  • CLASSPATH=classes:lib/dependency.jar java foo.bar.Baz
  • export CLASSPATH=classes:lib/dependency.jar; java foo.bar.Baz

Note that when using an executable jar (one with a Main-Class attribute in its manifest) it is necessary for the classpath to also be defined in the manifest and the command line classpath is ignored (see the java -jar documentation ).请注意,当使用可执行 jar(在其清单中具有 Main-Class 属性的 jar)时,还需要在清单中定义类路径,并忽略命令行类路径(请参阅java -jar 文档)。

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

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