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 Overflow
🌐
Coderanch
coderanch.com › t › 592115 › java › Explanation-java-cp-command
Explanation of java -cp command (Java in General forum at Coderanch)
September 10, 2012 - java tells the command line to execute java.exe, of course. -cp is an option that you feed to java.exe. It tells the JVM that you want to use a custom classpath, specified by the next command line argument. $JBOSS_HOME/bin/client/jboss-client.jar:. is the argument that goes with the -cp option.
Discussions

The -cp option to the java command - Unix & Linux Stack Exchange
java -cp FILE.jar FILE.Main inputfile What does the -cp option mean? For that matter, what a negative sign in front mean? I've searched everywhere and couldn't find the answer. More on unix.stackexchange.com
🌐 unix.stackexchange.com
October 2, 2013
Defining java -cp <path> in java code
Now, is there a way to simulate this argument within my java code? Why? Batch (CMD) files on Windows and Shell Scripts on *nix are a thing. Throw whatever command you need into a batch file/shell script, best with relative paths and you're good to go. Alternatively, you could create a launcher - pretty much like the Minecraft launcher. More on reddit.com
🌐 r/javahelp
6
0
July 21, 2022
stanford nlp - What does java -cp "*" mean? - Stack Overflow
If -classpath and -cp are not used and CLASSPATH is not set, then the user class path consists of the current directory (.). As a special convenience, a class path element that contains a base name of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. A Java ... More on stackoverflow.com
🌐 stackoverflow.com
[JAVA] What does the command -cp stand for in the command line?
It tells the Java interpreter where to look for class files. "cp" is for "class path". This is in the manual. More on reddit.com
🌐 r/learnprogramming
2
3
January 8, 2015
🌐
Medium
medium.com › @rostyslav.ivankiv › what-is-java-classpath-what-every-developer-should-know-e5f648bde862
What is Java Classpath? What every developer should know | by Rostyslav Ivankiv | Medium
March 5, 2023 - javac -cp "lib/commons-lang3-3.12.0.jar:lib/javax.json-1.1.4.jar" src/main/java/**/*.java -d target/classes java -cp "lib/commons-lang3-3.12.0.jar:lib/javax.json-1.1.4.jar:target/classes" edu.example.Main
🌐
Tech Monitor
techmonitor.ai › what-is › what-is-java-cp
What is Java -cp? - Tech Monitor
June 27, 2023 - Java -cp is a parameter in the Java Virtual Machine or Java compiler that specifies the location of classes and packages defined by the user.
🌐
Reddit
reddit.com › r/javahelp › defining java -cp in java code
r/javahelp on Reddit: Defining java -cp <path> in java code
July 21, 2022 -

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!

Top answer
1 of 4
2
Now, is there a way to simulate this argument within my java code? Why? Batch (CMD) files on Windows and Shell Scripts on *nix are a thing. Throw whatever command you need into a batch file/shell script, best with relative paths and you're good to go. Alternatively, you could create a launcher - pretty much like the Minecraft launcher.
2 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Oreate AI
oreateai.com › blog › understanding-the-java-cp-command-parameter › cdb3eaa20e49e76bdbfd5c0cb5e855c2
Understanding the Java -Cp Command Parameter - Oreate AI Blog
December 22, 2025 - First, let's understand what -cp does: It is used in Java's command line to specify a classpath—the path that contains one or more directories with class files needed by a Java program during execution.
Find elsewhere
🌐
MIT
web.mit.edu › 6.031 › www › fa17 › projects › fb1 › commandline.html
Running Java Programs with Command-Line Arguments
java -cp bin:lib/parserlib.jar:lib/physics.jar some.package.Main filename1 filename2 …
🌐
JanBask Training
janbasktraining.com › community › java › what-does-the-java-cp-option-do
What does the java cp option do? | JanBask Training Community
October 13, 2022 - Specifies a list of directories, JAR files, and ZIP archives to search for class files. Separate classpath entries with semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - The -classpath option is the recommended option for changing class path settings, because each application can have the class path it needs without interfering with any other application.The java command also has a -cp option that is an abbreviation for -classpath.
🌐
Lenovo
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo US
On Unix-based systems, you can use echo $CLASSPATH, while on Windows, you can use echo %CLASSPATH%. Alternatively, for a specific Java program, running java -cp or checking IDE configurations can reveal the classpath currently in use for that compilation or execution instance.
🌐
Baeldung
baeldung.com › home › java › run a java application from the command line
Run a Java Application from the Command Line | Baeldung
February 20, 2026 - We’ll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As we can see, in this case, we’ll have to include the main class name ...
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path. The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option.
🌐
Reddit
reddit.com › r/learnprogramming › [java] what does the command -cp stand for in the command line?
r/learnprogramming on Reddit: [JAVA] What does the command -cp stand for in the command line?
January 8, 2015 -

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!

🌐
Coderanch
coderanch.com › t › 736630 › java › cp-javac-command
How to use -cp in javac command? (Beginning Java forum at Coderanch)
November 13, 2020 - The Unix/Linux syntax would be "javac -cp ..:. ClassB.java". Actually, "..\." and ".." are equivalent. Note that the semicolon between ".." and "." becomes a colon, since the semi-colon is used to separate multiple commands on a Unix shell command line. Windows couldn't use ":" since if your class director was named "C", then "cp C:foo" would be misread, if you actually meant "C;foo" (two separate directories).
🌐
HowToDoInJava
howtodoinjava.com › home › java examples › java – set classpath from command line
Java - Set Classpath from Command Line
January 25, 2022 - java -cp c:/temp/lib applation.jar //includes all classes in the directory 'c:/temp/lib' java -cp c:/temp/lib/* application.jar //includes all jars in the directory 'c:/temp/lib' java -cp c:/temp/lib;c:/temp/lib/* application.jar //includes all classes and jars
🌐
Reddit
reddit.com › r/javahelp › classpath issue (command line cp def conflicting with jar manifest)?
r/javahelp on Reddit: classpath issue (command line CP def conflicting with jar manifest)?
September 21, 2023 -

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.)

Top answer
1 of 3
2
If you use -cp, the manifest's classpath isn't relevant. If you use -jar, the -cp isn't relevant. And for the manifest, the classpath entries are relative to the starting directory. This is documented behavior.
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › specs › man › java.html
The java Command
January 20, 2026 - javac -d <memory> HelloWorld.java java -cp <memory> hello.World