是否有一种方法将所有jar文件包含在类路径的目录中?
我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?
是否有一种方法将所有jar文件包含在类路径的目录中?
我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?
当前回答
我知道的唯一方法是单独做,例如:
setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.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
如果您在Eclipse或Netbeans等任何IDE之外开发和运行Java应用程序,以上所有解决方案都非常有效。
如果您在Windows 7上使用Eclipse IDE for Development in Java,那么如果使用命令提示符运行Eclipse内部构建的类文件,可能会遇到问题。
例如,你在Eclipse中的源代码具有以下包层次结构: edu.sjsu.myapp.Main.java
json.jar是Main.java的外部依赖项
当您尝试从Eclipse中运行Main.java时,它将毫无问题地运行。
但是当你在Eclipse中编译Main.java后尝试使用命令提示符运行这个命令时,它会抛出一些奇怪的错误,比如“ClassNotDef Error blah blah”。
我假设你在源代码的工作目录中!!
使用下面的语法从命令提示符运行它:
json.jar" Main.java .jar" java -cp ".;json.jar" edu.sjsu.myapp.Main 不要错过。以上)
这是因为您已经将Main.java放在包edu.sjsu.myapp中,java.exe将查找确切的模式。
希望能有所帮助!!
我在一个文件夹里有多个罐子。下面的命令在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
简单回答:java -classpath lib/*:。my.package.Program
Oracle提供了关于在Java 6和Java 7的类路径中使用通配符的文档,在“理解类路径通配符”一节中。(在我写这篇文章的时候,这两页的内容是一样的。)以下是此次会议的重点总结:
In general, to include all of the JARs in a given directory, you can use the wildcard * (not *.jar). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name. The above two options can be combined to include all JAR and class files in a directory, and the usual classpath precedence rules apply. E.g. -cp /classes;/jars/* The wildcard will not search for JARs in subdirectories. The above bullet points are true if you use the CLASSPATH system property or the -cp or -classpath command line flags. However, if you use the Class-Path JAR manifest header (as you might do with an ant build file), wildcards will not be honored.
是的,我的第一个链接与得分最高的答案中提供的链接相同(我没有希望超越它),但这个答案并没有提供更多的解释。由于现在Stack Overflow不鼓励这种行为,我想我应该对此进行扩展。
我知道的唯一方法是单独做,例如:
setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.
希望有帮助!