Differences between "java -cp" and "java -jar"?

What is the difference between starting a Java application using java -cp CLASSPATH and java -jar JAR_FILE_PATH ? Does one of them prefer the other to run a Java application? I mean, which of these methods is more expensive for the JVM (depending on the use of their resources)?

Which one will cause the JVM to create new threads when trying to start the application?

+63
java classpath jar jvm
Aug 12 2018-12-12T00:
source share
6 answers

I prefer the first version to run the Java application only because it has fewer traps ("welcome to the hellish path to the class"). The second requires a jar executable, and the class path for this application must be defined inside the jar manifest (all other classpath declarations will be ignored silently). So, with the second version you will need to look into the jar, read the manifest and try to find out if the classpath elements from which the jar is stored are valid ... This can be avoided.

I do not expect any performance advantages or disadvantages for any version. It just tells jvm which class to use for the main thread and where it can find the libraries.

+54
Aug 12 2018-12-12T00:
source share

With the -cp argument -cp you provide path paths to the path to other classes or libraries that your program may require when compiling or starting it. With -jar you specify the executable JAR file that you want to run.

You cannot specify both of them. If you try to run java -cp folder/myexternallibrary.jar -jar myprogram.jar , then this will not work. The class path for this JAR must be specified in its manifest, and not as the -cp argument.

Read more about it here and here .

PS: -cp and -classpath are synonyms.

+43
Aug 12 2018-12-12T00:
source share

java -cp CLASSPATH is necessary if you want to specify all the code in the classpath. This is useful for debugging code.

Bright executable format: java -jar JarFile can be used if you want to start the application with one short command. You can specify additional dependent jar files in your MANIFEST using space separators in the Class-Path entry, for example:

 Class-Path: mysql.jar infobus.jar acme/beans.jar 

Both are comparable in terms of performance.

+10
Aug 12 2018-12-12T00:
source share

When using java -cp you need to provide the full name of the main class, e.g.

java -cp com.mycompany.MyMain

When using java -jar myjar.jar your jar file should provide information about the main class through manifest.mf contained in the jar file in the META-INF folder:

Main-Class: com.mycompany.MyMain

+10
Aug 12 2018-12-12T00:
source share

In terms of performance, there will be no difference. Using java-cp, we can specify the necessary classes and jar in the classpath to run the java class file.

If it is an executable jar file. When using the java -jar command, jvm finds the class that should be run from the / META -INF / MANIFEST.MF file inside the jar file.

+1
Aug 12 '12 at 15:57
source share

As already mentioned, -cp is intended only to tell jvm on the command line which class to use for the main stream and where it can find libraries (define classpath). In -jar, it expects the path class and main class to be defined in the manifest of the jar file. So the other is for defining things on the command line, while others discover them inside the jar manifest. There is no difference in performance. You cannot use them at the same time, -jar will override -cp.

Although even if you use -cp, it will still check the manifest file. This way you can define some of the class classes in the manifest, and some on the command line. This is especially useful if you have a dependency on any third-party jar that you cannot provide to your assembly or do not want to provide (expecting that it will be found already in the system where it will be installed, for example). Therefore, you can use it to provide external cans. It may vary between systems or may even have a different version on another system (but with the same interfaces). Thus, you can create an application with a different version and add the actual third-party dependency to the class path on the command line when it starts on different systems.

0
Feb 03 '17 at 7:21
source share



All Articles