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

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


当前回答

java命令的参数顺序也很重要:

c:\projects\CloudMirror>java Javaside -cp "jna-5.6.0.jar;.\"
Error: Unable to initialize main class Javaside
Caused by: java.lang.NoClassDefFoundError: com/sun/jna/Callback

c:\projects\CloudMirror>java -cp "jna-5.6.0.jar;.\" Javaside
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable

其他回答

对我来说,这适用于窗户。

java -cp "/lib/*;" sample

linux

java -cp "/lib/*:" sample

我使用的是Java 6

来自wepapp的类:

  > mvn clean install

  > java -cp "webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/tool-jar-1.17.0-SNAPSHOT.jar;webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/*" com.xx.xx.util.EncryptorUtils param1 param2

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

这不是一个能够将/*设置为-cp的直接解决方案,但我希望您可以使用下面的脚本来缓解动态类路径和lib目录的情况。

 libDir2Scan4jars="../test";cp=""; for j in `ls ${libDir2Scan4jars}/*.jar`; do if [ "$j" != "" ]; then cp=$cp:$j; fi; done; echo $cp| cut -c2-${#cp} > .tmpCP.tmp; export tmpCLASSPATH=`cat .tmpCP.tmp`; if [ "$tmpCLASSPATH" != "" ]; then echo .; echo "classpath set, you can now use  ~>         java -cp \$tmpCLASSPATH"; echo .; else echo .; echo "Error please check libDir2Scan4jars path"; echo .; fi; 

为Linux编写脚本,也可以为windows编写类似的脚本。如果提供了正确的目录作为"libDir2Scan4jars"的输入;脚本将扫描所有的jar并创建一个类路径字符串,并将其导出到一个env变量“tmpCLASSPATH”。

macOS,当前文件夹

Java 13在macOS Mojave…

如果所有的.jar文件都在同一个文件夹中,使用cd将其设置为当前工作目录。与pwd核实。

对于-classpath,首先必须列出应用程序的JAR文件。使用冒号:作为分隔符,附加星号*以获得同一文件夹中的所有其他JAR文件。最后,将类的完整包名与main方法一起传递。

例如,对于一个名为my_app.jar的JAR文件中的一个应用程序,在一个名为com的包中一个名为app的类中有一个主方法。例如,在同一个文件夹中放置一些需要的罐子:

java -classpath my_app.jar:* com.example.App