是否有一种方法将所有jar文件包含在类路径的目录中?

我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?


当前回答

窗口:

 java -cp file.jar;dir/* my.app.ClassName

Linux:

 java -cp file.jar:dir/* my.app.ClassName

提醒: - Windows路径分隔符为; —Linux路径分隔符为: —在Windows操作系统中,如果cp参数不包含空格,“quotes”为可选参数

其他回答

如果使用Java 6,则可以在类路径中使用通配符。

现在可以在类路径定义中使用通配符:

javac -cp libs/* -verbose -encoding UTF-8 src/mypackage/*.java  -d build/classes

裁判:http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

我在一个文件夹里有多个罐子。下面的命令在JDK1.8中对我有用,可以包括文件夹中的所有jar。请注意,如果在类路径中有空格,请在引号中包含

窗户

“C:\My Jars\sdk\lib\*

运行:java -classpath "C:\My Jars\sdk\lib\*; C:\ programs" MyProgram

Linux

编译:javac -classpath "/home/guestuser/My Jars/sdk/lib/*" MyProgram.java

运行:java -classpath "/home/guestuser/My Jars/sdk/lib/*:/home/guestuser/programs" MyProgram

致相关人士:

我在Windows MSYS/MinGW shell下发现了这种奇怪的行为。

工作原理:

$ javac -cp '.;c:\Programs\COMSOL44\plugins\*' Reclaim.java

不工作:

$ javac -cp 'c:\Programs\COMSOL44\plugins\*' Reclaim.java
javac: invalid flag: c:\Programs\COMSOL44\plugins\com.comsol.aco_1.0.0.jar
Usage: javac <options> <source files>
use -help for a list of possible options

我很确定通配符没有被shell扩展,因为例如。

$ echo './*'
./*

(我也用另一个程序试过,而不是内置的echo,结果是一样的。)

我相信是javac试图展开它,无论参数中是否有分号,它的行为都是不同的。首先,它可能试图展开所有看起来像路径的参数。只有这样,它才会解析它们,-cp只接受以下标记。(注意com.comsol.aco_1.0.0.jar是该目录中的第二个JAR。)这都是猜测。

这是

$ javac -version
javac 1.7.0

您需要分别添加它们。或者,如果确实需要指定一个目录,可以将所有内容解jar到一个目录中,并将其添加到类路径中。但是,我不推荐这种方法,因为您可能会在类路径版本控制和不可管理性方面出现奇怪的问题。

使用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