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.
Answer from Andreas Dolk on Stack OverflowI 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.
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.
[JAVA] What does the command -cp stand for in the command line?
classpath issue (command line CP def conflicting with jar manifest)?
Defining java -cp <path> in java code
classpath - How do you pass multiple paths to Java -cp command? - Stack Overflow
Videos
The example you finally gave, -cp is a parameter to the command, which is java. Parameters are generally program-specific, in this case cp stands for Class Path, which is another location java will search to find the class files as they are needed by the program.
See this:
- http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
Quote:
-classpath classpath -cp classpath
Specifies a list of directories, JAR files, and ZIP archives to search for class files. Separate class path entries with semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
I've started a java course in university and they teach when you run a java file you type "java -cp . HelloWorldApp".
I have always typed "java HelloWorldApp" and the program has always run fine for me. What's the difference?
Thanks!
Let's say I have a folder structure
/lib/A/a.jar
/lib/B/b.jar
/lib/C/c.jar
program.jar
program.bat
Now, my bat file defines the classpath when it calls java as so:
java.exe -cp program.jar;lib/A/*;lib/B/*;lib/C/* EntryClassWithMain
Now, let's also say that a.jar has a manifest in it that defines it's classes as:
Class-Path: b.jar c.jar
Now, would the VM known where the b.jar and c.jar are that a.jar references in it's manifest? I thoguht the fact that these files are defined by the classpath provided to java.exe would cover this. Our would the manifest's definition mean that b.jar and c.jar must be in lib/A
This is an actual real world issue. Deployments for testing take a while (hours) to get deployed at my company. So trying to understand this most importantly. But also don't want to change our folder structure that has worked fine for years till recently. The whole manifest defining the classpath is new thing one of our teams started doing. (I'm not responsible for a.jar, b.jar and c.jar. And I greatly simplified this example.)
Hi, I learned from this (https://stackoverflow.com/questions/39839891/) stack overflow post that it is not possible to declare a folder where multiple unknown jars are in as dependencies inside of the Manifest. The answer marked as solution stated it was possible to achieve similar by executing the jar file with:
java -cp <path> -jar <file>
Though I don't want the user to have to type that in everytime my jar gets run. Now, is there a way to simulate this argument within my java code? Thanks in advance!