是否有一种方法将所有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 -cp file.jar;dir/* my.app.ClassName
Linux:
java -cp file.jar:dir/* my.app.ClassName
提醒: - Windows路径分隔符为; —Linux路径分隔符为: —在Windows操作系统中,如果cp参数不包含空格,“quotes”为可选参数
如果您确实需要动态指定所有的.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>
在启动构建文件中,该文件将使用正确的类路径启动应用程序。
我知道的唯一方法是单独做,例如:
setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.
希望有帮助!
简单回答: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不鼓励这种行为,我想我应该对此进行扩展。
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