1

What is the difference in the running of the java program in Maven:

1: java -jar target/join-1.0-SNAPSHOT.jar ...

2: java -cp target/MavenTestApp-1.0-SNAPSHOT.jar org.koushik.javabrains.App

I think for the first one I need to have jar. Maybe it is connected with operating system. java -jar is on windows but java -cp is on linux or it does not matter? Thanks

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
user3455638
  • 569
  • 1
  • 6
  • 17

2 Answers2

4

The difference is in how JVM learns the start-up class (i.e. the one from which it takes the public static main(String[]) method that needs to run first).

  • With the -cp option you provide the name of the class on the command line
  • With the -jar option, the name of the class is taken from the manifest file inside the JAR; the class path, if any, is also discarded.

Here is the documentation that explains how the entry point is set with the manifest.

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

Main-Class: classname

The value classname is the name of the class that is your application's entry point.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

In the first one, you are executing the default main class mentioned in manifest file of the jar whereas in the second one, the .jar file is kept in classpath and the name of the main class to be executed is mentioned

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53