Does java -jar option alter classpath options

别说谁变了你拦得住时间么 提交于 2019-12-19 07:26:29

问题


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?


回答1:


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.




回答2:


You will need your own classloader to deal with this. -jar only respects the information in the Manifest and wildcards are not allowed there.

You might find the example of a reloadable class useful: http://www.exampledepot.com/egs/java.lang/ReloadClass.html




回答3:


Here is a good discussion on this issue.



来源:https://stackoverflow.com/questions/8396100/does-java-jar-option-alter-classpath-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!