153

What is the difference between running a Java application with
java -cp CLASSPATH and java -jar JAR_FILE_PATH? Is one of them preferred to the other for running a Java application? I mean which one of these ways is more expensive for JVM (according to their machine resources usage)?

Which one will cause JVM to spawn more threads while trying to run the application?

Reza
  • 2,058
  • 3
  • 20
  • 33

6 Answers6

119

I prefer the first version to start a java application just because it has less pitfalls ("welcome to classpath hell"). The second one requires an executable jar file and the classpath for that application has to be defined inside the jar's manifest (all other classpath declaration will be silently ignored...). So with the second version you'd have to look into the jar, read the manifest and try to find out if the classpath entries are valid from where the jar is stored... That's avoidable.

I don't expect any performance advantages or disadvantages for either version. It's just telling the jvm which class to use for the main thread and where it can find the libraries.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • What about the threads? Are they the same in terms of number of threads JVM spawn while trying to run the application? – Reza Aug 12 '12 at 14:22
  • 1
    Yes. Both versions will locate the main method and execute that with a single thread. The first version passes the classname as an argument, with the second version, the jvm will find it inside the mainfest. – Andreas Dolk Aug 12 '12 at 14:30
  • @Andreas_D Would please take a look at this [post](https://stackoverflow.com/q/59221800/12493832), I guess I am using -cp in a wrong way, and I don't how to correct my mistake. – JJJohn Dec 07 '19 at 00:59
76

With the -cp argument you provide the classpath i.e. path(s) to additional classes or libraries that your program may require when being compiled or run. With -jar you specify the executable JAR file that you want to run.

You can't specify them both. If you try to run java -cp folder/myexternallibrary.jar -jar myprogram.jar then it won't really work. The classpath for that JAR should be specified in its Manifest, not as a -cp argument.

You can find more about this here and here.

PS: -cp and -classpath are synonyms.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
  • My concern is about the resources? is there a difference between resources they use? – Reza Aug 12 '12 at 13:58
  • @Hesam If you're asking about the performance differences between `-cp` and `-classpath`, then no, there is no difference. – Radu Murzea Aug 12 '12 at 13:59
  • 1
    No! I meant the performance difference between -cp (or -classpath) and -jar – Reza Aug 12 '12 at 14:09
  • 2
    @Hesam With `-jar` you specify what executable JAR you want to run. With `-cp` you specify path(s) to additional classes/library that your program may require. The 2 have very different purposes. – Radu Murzea Aug 12 '12 at 14:22
  • @RaduMurzea, What do you mean it wouldn't *really* work when we combine them? E.g. `java -jar PATH -cp PATH2` – Pacerier Aug 23 '14 at 12:35
  • @Radu Murzea in one video tutorial I watched, he run this command, `java -cp target/jarName.jar org.packageName.javaApp` is this correct. if it is then what is he doing, is he just specifying the classpath inside the jar file – Kasun Siyambalapitiya Jan 06 '17 at 11:54
  • @Radu Murzea Would please take a look at this [post](https://stackoverflow.com/q/59221800/12493832), I guess I am using -cp in a wrong way, and I don't how to correct my mistake. – JJJohn Dec 07 '19 at 01:00
26

When using java -cp you are required to provide fully qualified main class name, e.g.

java -cp com.mycompany.MyMain

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

Main-Class: com.mycompany.MyMain

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Would please take a look at this [post](https://stackoverflow.com/q/59221800/12493832), I guess I am using -cp in a wrong way, and I don't how to correct my mistake. – JJJohn Dec 07 '19 at 01:00
11

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

The jarred executable format: java -jar JarFile can be used if you wish to start the app with a single short command. You can specify additional dependent jar files in your MANIFEST using space separated jars in a Class-Path entry, e.g.:

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

Both are comparable in terms of performance.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Would please take a look at this [post](https://stackoverflow.com/q/59221800/12493832), I guess I am using -cp in a wrong way, and I don't how to correct my mistake. – JJJohn Dec 07 '19 at 01:00
2

Like already said, the -cp is just for telling the jvm in the command line which class to use for the main thread and where it can find the libraries (define classpath). In -jar it expects the class-path and main-class to be defined in the jar file manifest. So other is for defining things in command line while other finding them inside the jar manifest. There is no difference in performance. You can't use them at the same time, -jar will override the -cp.

Though even if you use -cp, it will still check the manifest file. So you can define some of the class-paths in the manifest and some in the command line. This is particularly useful when you have a dependency on some 3rd party jar, which you might not provide with your build or don't want to provide (expecting it to be found already in the system where it's to be installed for example). So you can use it to provide external jars. It's location may vary between systems or it may even have a different version on different system (but having the same interfaces). This way you can build the app with other version and add the actual 3rd party dependency to class-path on the command line when running it on different systems.

Pehmolelu
  • 3,534
  • 2
  • 26
  • 31
1

There won't be any difference in terms of performance. Using java - cp we can specify the required classes and jar's in the classpath for running a java class file.

If it is a executable jar file . When java -jar command is used, jvm finds the class that it needs to run from /META-INF/MANIFEST.MF file inside the jar file.