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

[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
classpath issue (command line CP def conflicting with jar manifest)?
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. More on reddit.com
🌐 r/javahelp
7
0
September 21, 2023
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
classpath - How do you pass multiple paths to Java -cp command? - Stack Overflow
So I am trying to compile a file that imports code from 2+ different .jar files. The following is the command I am using to compile my file: javac -cp /home/ugrads/majors/quinnliu/workspace/Walnu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Coderanch
coderanch.com › t › 736630 › java › cp-javac-command
How to use -cp in javac command? (Beginning Java forum at Coderanch)
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).
🌐
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.
🌐
Oreate AI
oreateai.com › blog › java-cp-command-parameter › 09960d5e231f462ecd5672bc296ca578
Java -Cp Command Parameter - Oreate AI Blog
December 22, 2025 - In Java, the -cp command parameter is used to specify the class path or JAR file paths. This parameter informs the JVM where to look for classes that need to be loaded. The -cp option should be followed by a list of paths separated by delimiters ...
🌐
MIT
web.mit.edu › 6.031 › www › fa17 › projects › fb1 › commandline.html
Running Java Programs with Command-Line Arguments
Open a command prompt and cd into your project folder. Then run the java command with these arguments: java -cp bin:lib/parserlib.jar:lib/physics.jar some.package.Main filename1 filename2 …
Find elsewhere
🌐
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!

🌐
CodeJava
codejava.net › java-core › tools › examples-of-using-java-command
java command examples
August 4, 2019 - java -cp lib/mail.jar;. PlainTextEmailSenderIf the program depends on more than one jar files:
🌐
Tech Monitor
techmonitor.ai › what is
What is Java -cp? - Tech Monitor
March 2, 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.
🌐
Brainly
brainly.com › engineering › college › what is the java cp command?
[FREE] What is the Java CP command? - brainly.com
February 15, 2023 - It specifies the location of user-defined classes and packages that the Java runtime and compiler need to access when executing a Java program. To utilize the CP command, you can use it with the -cp or -classpath option followed by directories ...
🌐
HowToDoInJava
howtodoinjava.com › home › java examples › java – set classpath from command line
Java - Set Classpath from Command Line
January 25, 2022 - Learn to use the -classpath or -cp option in command prompt to set classpath from command prompt in Windows and Linux OS.
🌐
Madhadron
madhadron.com › programming › java_cli_tools.html
madhadron - Working with Java command line tools
Now, say that you have a library of class files in /lib/mylib, another in /lib/otherlib, and your own code in /home/me/myproject. How do you tell the virtual machine to load the classes in all of these locations? You list them all as arguments to -cp, separated by :. So you would run · java -cp /lib/mylib:/lib/otherlib:/home/me/myproject HelloWorld
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
The runtime tool java has a -cp option, as well. This option is an abbreviation for -classpath. For very special cases, both java and javac have options that let you change the path they use to find their own class libraries. The vast majority of users will never to need to use those options, ...
🌐
Coderanch
coderanch.com › t › 546994 › engineering › java-cp-windows-linux-differences
java -cp windows and linux differences (General Computing forum at Coderanch)
All, may be useful: classpath definitions in command line differs if you use windows o.s. or linux: windows: java -cp "/myJarDirectoryFullPath/theJarfileToInclude.jar;." myCompiledJavaProgram linux: java -cp "/myJarDirectoryFullPath/theJarfileToInclude.jar:." myCompiledJavaProgram note the ; in windows and the : in linux.
🌐
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.
🌐
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.
🌐
Baeldung
baeldung.com › home › java › core java › java classpath syntax in linux vs. windows
Java Classpath Syntax in Linux vs. Windows | Baeldung
January 8, 2024 - For example, the man page of the java command from the latest(ver.17) OpenJDK shows: –class-path classpath, -classpath classpath, or -cp classpath A semicolon (;) separated list of directories, JAR archives, and ZIP archives to search for class files.