是否有一种方法将所有jar文件包含在类路径的目录中?
我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?
是否有一种方法将所有jar文件包含在类路径的目录中?
我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?
当前回答
我们通过部署一个主jar文件myapp.jar来解决这个问题,该文件包含一个清单(manifest .mf)文件,该文件指定了一个类路径和其他所需的jar,然后这些jar与它一起部署。在这种情况下,您只需要在运行代码时声明java -jar myapp.jar。
因此,如果你将主jar部署到某个目录中,然后将从属jar部署到该目录下的lib文件夹中,清单如下所示:
Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 1.0.1
Class-Path: lib/dep1.jar lib/dep2.jar
注意:这是与平台无关的——我们可以使用相同的jar在UNIX服务器或Windows PC上启动。
其他回答
我们通过部署一个主jar文件myapp.jar来解决这个问题,该文件包含一个清单(manifest .mf)文件,该文件指定了一个类路径和其他所需的jar,然后这些jar与它一起部署。在这种情况下,您只需要在运行代码时声明java -jar myapp.jar。
因此,如果你将主jar部署到某个目录中,然后将从属jar部署到该目录下的lib文件夹中,清单如下所示:
Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 1.0.1
Class-Path: lib/dep1.jar lib/dep2.jar
注意:这是与平台无关的——我们可以使用相同的jar在UNIX服务器或Windows PC上启动。
使用Java 6或更高版本,类路径选项支持通配符。注意事项:
使用直引号(") 使用*,而不是*.jar
窗户
lib/*" my.package.MainClass . java -cp "Test.jar
Unix
java -cp "Test.jar:lib/*" my.package.MainClass .jar:lib/*
这类似于Windows,但使用:而不是;。如果你不能使用通配符,bash允许以下语法(其中lib是包含所有Java存档文件的目录):
Java -cp "$(printf %s: lib/*.jar)"
(注意,使用类路径与-jar选项不兼容。参见:从命令提示符执行带有多个类路径库的jar文件)
理解通配符
从类路径文档中:
Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa. Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc. The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path. Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path. The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.
注意:由于java 8中的一个已知错误,windows示例必须在条目前使用反斜杠,后面带星号:https://bugs.openjdk.java.net/browse/JDK-8131329
正确的:
java -classpath "lib/*:." my.package.Program
不正确的:
java -classpath "lib/a*.jar:." my.package.Program
java -classpath "lib/a*:." my.package.Program
java -classpath "lib/*.jar:." my.package.Program
java -classpath lib/*:. my.package.Program
对于windows,引号是必需的,并且;应作为分离器使用。例如:
java -cp "target\\*;target\\dependency\\*" my.package.Main
如果您确实需要动态指定所有的.jar文件,您可以使用shell脚本或Apache Ant。有一个名为commons Launcher的公共项目,基本上可以让你将启动脚本指定为蚂蚁构建文件(如果你明白我的意思)。
然后,你可以指定如下内容:
<path id="base.class.path">
<pathelement path="${resources.dir}"/>
<fileset dir="${extensions.dir}" includes="*.jar" />
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
在启动构建文件中,该文件将使用正确的类路径启动应用程序。