Java -jar parameter changes classpath parameters

I have a jar file that mentions the main class in the manifest. When I try to execute a jar using the following command

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

The code runs and works.

When I try to execute a jar using the following command

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

I get the not found execptions class for some jars that are in the same folder as myjar.jar. I hope the -cp option will include these banks in the class path. I changed my code to print the java.class.path property. In the first case, he listed all the banks in the current directory, in the second case, he simply listed myjar.jar

I also modified the manifest to add a Class-Path element to it with all the banks. Then the second team works. But in my code I am trying to load the aribtrary class, whose name is indicated on the command line, so I want the class path to contain all the banks in the folder. How to get the second team to work in this scenario?

+6
source share
3 answers

From this ,

An executable JAR must reference all other dependent JAR files required through the header of the Path class of the manifest file. the CLASSPATH environment variable and any class path specified on the command line is ignored by the JVM if the -jar option is used.

+6
source

You will need your own class loader to handle this. -jar respects only the information in the manifest, and wildcards are not allowed there.

You can find an example of a useful reload class: http://www.exampledepot.com/egs/java.lang/ReloadClass.html

0
source

Here is a good discussion of this issue.

0
source

Source: https://habr.com/ru/post/903166/


All Articles