Java.class.path does not bring Manifest.mf Class-Path property

I am trying to get my way to the application class.

I have jar (named application.jar) and it has Manifest.mf files in its other jar files, for example Class-Path: a.jar b.jar .

Why, when I use System.getProperty("java.class.path") , my jars a.jar and b.jar are not specified?

+4
source share
2 answers

Perhaps this is due to the fact that "java.class.path" is a system property that is set from the classpath environment variable ($ CLASSPATH or -classpath). They are ignored using the -jar option.

According to the java -jar documentation, when this jar parameter is used to run the application, only the Class-Path manifest is considered, and other parameters are ignored. From http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html :

-jar

Run a program encapsulated in a JAR file. The first argument is the name of the JAR file instead of the name of the launch class. For this parameter to work, the JAR file manifest must contain a line of the form Main-Class: classname. Here, classname identifies a class that has a public static void main (String [] args) method that serves as the starting point of your application. For information on working with Jar files and Jar file manifests, see the Jar tool man page and the Jar trail Java tutorial.

When you use this parameter, the JAR file is the source of all user classes, and other paths of the user class path are ignored.

+2
source

This was a question that I also came to. Even if when using java -cp ..;myTest.jar test2.Needer I get only "..; myTest.jar" as a result for the java.class.path property.

Note. Even when you use the -cp parameter, the specified class path is searched in MANIFEST.MF ! (Could not find this information on Google and verify itself)

Therefore, I do not think this has anything to do with the -jar parameter. In Link you can find

Wildcard expansion is performed earlier than the main program method call, and not late, during the class loading process itself.

Interestingly, I found out during my tests: the classpath in MANFIFEST.MF is usually recursive. Therefore, if there is a specified test.jar in the MANIFEST.MF myTest.jar test.jar file. The file is in the class path, the class path in the MANIFEST.MF test.jar file will also be considered (using java -cp "myTest.jar" test2.Needer )

As a result, when the java.class.path property supports showing the path to the MANIFEST.MF classes, it should also display classpathes of all subsequently dependent .jar files. Since the class path is only executed until the classes are found, this will not apply well to the lazy loading mechanism.

TL DR : I think this has nothing to do with the -jar Parameter ( -cp ) parameter. In my explanation, the support for showing the classpath from MANIFEST.MF will only come with additional meaningless recursive search costs (because it does not have to be an actual dependency, with respect that is used from .jar). And since this meaningless search will delay the start of the program (since the recursive search can be really deep), it is not implemented.

0
source

All Articles