简体   繁体   English

java -jar选项是否改变了classpath选项

[英]Does java -jar option alter classpath options

I have a jar file which mentions the main class in the manifest. 我有一个jar文件,它提到了清单中的主要类。 When I try to execute the jar using the following command 当我尝试使用以下命令执行jar时

java -cp .;./* com.foo.MainClass

The code executes and works. 代码执行并运行。

When I try to execute the jar using the following command 当我尝试使用以下命令执行jar时

java -cp .;./* -jar myjar.jar

I get class not found execptions for some jars which are in the same folder as myjar.jar. 我找到了一些与myjar.jar在同一个文件夹中的jar的类别找不到的execptions。 I hoping that the -cp option will include those jars in class path. 我希望-cp选项将在类路径中包含这些jar。 I modified my code to print java.class.path property. 我修改了我的代码来打印java.class.path属性。 In the first case it listed all jars in the current directory, in second case it just listed myjar.jar 在第一种情况下,它列出了当前目录中的所有jar,在第二种情况下,它只列出了myjar.jar

I also modified the manifest to add Class-Path element to it with all jars. 我还修改了清单,以便为所有jar添加Class-Path元素。 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. 但是在我的代码中,我正在尝试加载一个名称在命令提示符下提供的aribtrary类,所以我希望类路径包含文件夹中的所有jar。 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. 可执行JAR必须通过清单文件的Class-Path标头引用它所需的所有其他依赖JAR。 The environment variable CLASSPATH and any class path specified on the command line is ignored by the JVM if the -jar option is used. 如果使用-jar选项,则JVM将忽略环境变量CLASSPATH和命令行上指定的任何类路径。

是对这个问题的一个很好的讨论。

You will need your own classloader to deal with this. 您将需要自己的类加载器来处理这个问题。 -jar only respects the information in the Manifest and wildcards are not allowed there. -jar只尊重清单中的信息,并且不允许使用通配符。

You might find the example of a reloadable class useful: http://www.exampledepot.com/egs/java.lang/ReloadClass.html 您可能会发现可重新加载的类的示例很有用: http//www.exampledepot.com/egs/java.lang/ReloadClass.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM