java -jar选项是否改变了classpath选项 [英] Does java -jar option alter classpath options
问题描述
我有一个jar文件,它提到了清单中的主要类。
当我尝试使用以下命令执行jar时
java -cp .; ./* com.foo .MainClass
代码执行并有效。
<当我尝试使用以下命令执行jar时
java -cp。; ./* -jar myjar.jar
我找不到与myjar.jar在同一文件夹中的某些jar的类别的execptions。我希望-cp选项将在类路径中包含这些jar。
我修改了我的代码以打印java.class.path属性。在第一种情况下,它列出了当前目录中的所有jar,在第二种情况下,它只列出了myjar.jar
我还修改了清单以向其添加Class-Path元素与所有罐子。然后第二个命令工作。但是在我的代码中,我正在尝试加载一个名称在命令提示符下提供的aribtrary类,所以我希望类路径包含文件夹中的所有jar。如何在这种情况下使第二个命令工作?
可执行JAR必须引用所有其他依赖项JARs
需要通过清单文件的Class-Path头。如果使用-jar选项,则JVM将忽略
环境变量CLASSPATH和
命令行上指定的任何类路径。
I have a jar file which mentions the main class in the manifest. When I try to execute the jar using the following command
java -cp .;./* com.foo.MainClass
The code executes and works.
When I try to execute the jar using the following command
java -cp .;./* -jar myjar.jar
I get class not found execptions for some jars which are in the same folder as myjar.jar. I hoping that the -cp option will include those jars in class path. I modified my code to print java.class.path property. In the first case it listed all jars in the current directory, in second case it just listed myjar.jar
I also modified the manifest to add Class-Path element to it with all jars. Then the second command works. But in my code I am trying to load a aribtrary class whose name is provided at command prompt, so I want the class path to contain all jars in a folder. How do I make the second command work in this scenario?
From this,
An executable JAR must reference all the other dependent JARs it requires through the Class-Path header of the manifest file. The environment variable CLASSPATH and any class path specified on the command line is ignored by the JVM if the -jar option is used.
这篇关于java -jar选项是否改变了classpath选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!