有没有一种方法可以在类路径中包含目录内的所有jar文件?
我正在尝试使用java -classpath lib/*.jar:. my.package.Program
,但它无法找到肯定在这些jar文件中的类文件。我需要单独将每个jar文件添加到类路径中吗?
有没有一种方法可以在类路径中包含目录内的所有jar文件?
我正在尝试使用java -classpath lib/*.jar:. my.package.Program
,但它无法找到肯定在这些jar文件中的类文件。我需要单独将每个jar文件添加到类路径中吗?
<path id="base.class.path">
<pathelement path="${resources.dir}"/>
<fileset dir="${extensions.dir}" includes="*.jar" />
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
敬启者:
我在MSYS/MinGW shell下的Windows系统中发现了一种奇怪的行为。
正常工作:
$ 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
java -cp "somewhere/*;"
对于在macOS Mojave上的Java 13...
如果您的所有.jar
文件都在同一个文件夹中,请使用cd
命令将其设置为您的当前工作目录。使用pwd
命令进行验证。
对于-classpath
参数,您必须首先列出应用程序所需的JAR文件。然后使用冒号字符:
作为分隔符,在末尾添加星号*
以获取同一文件夹中的所有其他JAR文件。最后,传递带有main
方法的类的完整包名称。
例如,如果一个名为my_app.jar
的JAR文件包含名为App
的类中的main
方法,并且该类位于名为com.example
的包中,同时在同一文件夹中还需要一些JAR文件:
java -classpath my_app.jar:* com.example.App
opt1: "Extract required libraries into generated JAR"
opt2: "Package required libraries into generated JAR"
opt3: "Copy required libraries into a sub-folder next to the generated JAR"
如果你将你的jar文件命名为:“fooBarTheJarFile.jar”,并且所有东西都设置为导出到目录:“/theFully/qualifiedPath/toYourChosenDir”。
(意味着“导出目标”字段将显示为:'/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar')
完成后,您会发现eclipse将所有库放入名为'fooBarTheJarFile_lib'的文件夹中,位于导出目录中,这样就会得到类似以下内容:
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar01.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar02.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar03.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar04.jar
java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*" -jar /theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar package.path_to.the_class_with.your_main.TheClassWithYourMain
java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar;/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*" somepackages.inside.yourJar.leadingToTheMain.TheClassWithYourMain
cd /theFully/qualifiedPath/toYourChosenDir/;
BREAKS: java -cp "fooBarTheJarFile_lib/*" package.path_to.the_class_with.your_main.TheClassWithYourMain
BREAKS: java -cp ".;fooBarTheJarFile_lib/*" package.path_to.the_class_with.your_main.TheClassWithYourMain
BREAKS: java -cp ".;fooBarTheJarFile_lib/*" -jar package.path_to.the_class_with.your_main.TheClassWithYourMain
WORKS: java -cp ".;fooBarTheJarFile_lib/*" -jar fooBarTheJarFile.jar package.path_to.the_class_with.your_main.TheClassWithYourMain
< p >(使用Java版本“1.6.0_27”;通过Ubuntu 12.04上的OpenJDK 64位服务器VM)< / p >
对于Windows系统,需要加上引号并使用分号作为分隔符。例如:
java -cp "target\\*;target\\dependency\\*" my.package.Main
从webapp中导入的类:
> 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
setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.
for jar in $(ls $HOME/bin/*.jar); do
export CLASSPATH=$jar:$CLASSPATH;
done
- Devon Peticolas您需要分别添加它们。或者,如果您确实只需指定一个目录,则可以将所有内容解压缩到一个目录中,并将其添加到类路径中。但我不建议使用这种方法,因为您会冒着类路径版本和无法管理的奇怪问题的风险。
jar:
后面的.
是错误的,但是...... 在 Unix 和 Windows 系统中,“当前目录”的标准符号是一个单点 (.)。 - KNUjava -classpath ".;lib/*" Program
。 - Evgeni Sergeev