JAVA difference between the two methods of execution jar

1 java -jar way

2 java -classpath way

The above two methods can not coexist, i.e. not yet specified parameters jar classpath parameter specifies the time for the following reasons:

In the absence of the specified custom class loader, class loader is the AppClassLoader two ways,

For the first way:

AppClassLoader dependent only load classes and the jar MANIFEST.MF file parameter specifies the jar in a jar in which:

Main-Class: test.someClassName

Class-Path: lib/some.jar

That is, this approach ignores the -classpath parameter (if also add this parameter) specified dependency jar.

In addition, when we loaded our engineering resource file in the jar inside the class through the AppClassLoader, AppClassLoader is not going to search for Class-Path path, but will only look for the jar file in the current resource. Namely: -jar only load resources from the current jar in the class path does not search!

For the second way:

AppClassLoader jar dependent only load classes and on the classpath specified path.

When we loaded our engineering resource file in my class what's inside through AppClassLoader, AppClassLoader is not going to search for the resource file from the jar, but only to find the resource files in the current directory in the class path. Namely: -classpath only loads the resource file from the class path, not a search jar.

Published 63 original articles · won praise 25 · views 80000 +

Guess you like

Origin blog.csdn.net/w1857518575/article/details/87169135