是否有一种方法将所有jar文件包含在类路径的目录中?
我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?
是否有一种方法将所有jar文件包含在类路径的目录中?
我正在尝试java -classpath lib/*.jar:。program,它不能找到在这些罐子里的类文件。我是否需要将每个jar文件分别添加到类路径中?
当前回答
来自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
其他回答
把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不鼓励这种行为,我想我应该对此进行扩展。
对我来说,这适用于窗户。
java -cp "/lib/*;" sample
linux
java -cp "/lib/*:" sample
我使用的是Java 6
简写形式:如果你的main在一个jar中,你可能需要一个额外的“-jar pathTo/yourJar/YourJarsName.jar”显式声明来让它工作(即使“YourJarsName.jar”在类路径中) (或者,回答5年前提出的原始问题:你不需要显式地重新声明每个jar,但看起来,即使在java6中,你也需要重新声明自己的jar…)
长形式: (我已经明确说明了这一点,我希望即使是java的闯入者也可以使用它)
像这里的许多人一样,我使用eclipse导出JAR:(File-> export ->'可运行的JAR文件')。eclipse (Juno)提供了三个“库处理”选项:
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"
通常我会使用opt2 (opt1肯定会破坏),但是我正在使用的一个jar中的本机代码,当您选择该选项时,我发现eclipse利用了方便的“jarinjar”技巧。即使在意识到我需要opt3之后,然后找到这个StackOverflow条目,我仍然花了一些时间来弄清楚如何在eclipse之外启动我的main,所以这里是对我有用的,因为它对其他人很有用……
如果你将罐子命名为fooBarTheJarFile.jar 和所有设置导出到目录:“/theFully/qualifiedPath/toYourChosenDir”。
(意味着“导出目的地”字段将读取:“/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar”)
当你点击finish后,你会发现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新手来说:` package.path_to.the_class_with. path `。your_main'是声明的包路径,你可以在' classwithyourmain .java'文件的顶部找到,该文件包含'main(String[] args){…}'你希望在java之外运行)
要注意的陷阱:在你声明的类路径上的jar列表中有'fooBarTheJarFile.jar'是不够的。您需要显式地声明'-jar',并重新声明该jar的位置。
例如:
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
(使用Java版本“1.6.0_27”;通过OpenJDK 64位服务器虚拟机安装在ubuntu 12.04上)
如果使用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/