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.

Answer from iain on Stack Exchange
🌐
Coderanch
coderanch.com › t › 736630 › java › cp-javac-command
How to use -cp in javac command? (Beginning Java forum at Coderanch)
Hi, The book gives an example of -cp using the java run command: java -cp classes packageb.ClassB It worked.
🌐
Coderanch
coderanch.com › t › 592115 › java › Explanation-java-cp-command
Explanation of java -cp command (Java in General forum at Coderanch)
September 10, 2012 - For example, are you really typing: java -cp $JBOSS_HOME/bin/client/jboss-client.jar:. test.Client.class ... instead of: java -cp $JBOSS_HOME/bin/client/jboss-client.jar:. test.Client Note that the java command takes a class name, not the filename of a class file.
Discussions

command line - Including all the jars in a directory within the Java classpath - Stack Overflow
Remind: - Windows path separator is ; - Linux path separator is : - In Windows if cp argument does not contains white space, the "quotes" is optional ... windows example doesn't work for java 8 and earlier: see bugs.openjdk.java.net/browse/JDK-8131329 2018-07-18T16:34:37.393Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
Differences between "java -cp" and "java -jar"? - Stack Overflow
This is particularly useful when ... 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. ... There won't be any difference in terms of performance. Using java - cp we can specify ... More on stackoverflow.com
🌐 stackoverflow.com
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
Running Java in Command Prompt?
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. More on reddit.com
🌐 r/javahelp
11
1
February 2, 2021
Top answer
1 of 16
1340

Using Java 6 or later, the classpath option supports wildcards. Note the following:

  • Use straight quotes (")
  • Use *, not *.jar

Windows

java -cp "Test.jar;lib/*" my.package.MainClass

Unix

java -cp "Test.jar:lib/*" my.package.MainClass

This is similar to Windows, but uses : instead of ;. If you cannot use wildcards, bash allows the following syntax (where lib is the directory containing all the Java archive files):

java -cp "$(printf %s: lib/*.jar)"

(Note that using a classpath is incompatible with the -jar option. See also: Execute jar file with multiple classpath libraries from command prompt)

Understanding Wildcards

From the Classpath document:

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. 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. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

Note: due to a known bug in java 8, the windows examples must use a backslash preceding entries with a trailing asterisk: https://bugs.openjdk.java.net/browse/JDK-8131329

2 of 16
287

Under Windows this works:

java -cp "Test.jar;lib/*" my.package.MainClass

and this does not work:

java -cp "Test.jar;lib/*.jar" my.package.MainClass

Notice the *.jar, so the * wildcard should be used alone.


On Linux, the following works:

java -cp "Test.jar:lib/*" my.package.MainClass

The separators are colons instead of semicolons.

🌐
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:
🌐
Oreate AI
oreateai.com › blog › java-cp-command-parameter › 09960d5e231f462ecd5672bc296ca578
Java -Cp Command Parameter - Oreate AI Blog
December 22, 2025 - Each path can either be a directory or a JAR file. Here are some examples of using the -cp command: - java -cp /path/to/classes MainClass: Specifies the class path as /path/to/classes and executes the MainClass.
🌐
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.
Find elsewhere
🌐
Java CP Algos
javacpalgos.com › home › java cp – classpath
Java CP - CLASSPATH - Java CP Algos
February 9, 2022 - In the above example ABC will be looked first and then XYZ. The java -cp command is used to deliver the classpath or directories to other classes or libraries that our code might use ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › linux
Linux cp command - Java Code Geeks
July 18, 2022 - There are multiple options for the copy command. cp [OPTION] SOURCE DESTINATION – the simple command to copy one source to the destination
🌐
Brainly
brainly.com › engineering › college › what is the java cp command?
[FREE] What is the Java CP command? - brainly.com
February 15, 2023 - Understanding how to use the CP ... all the required resources. An example of using the CP command is: java -cp C:\myclasses;C:\otherclasses MyClass, which allows the program to find classes in the specified directo...
🌐
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 · To unset a classpath value previously set, use this command to clear its value using am empty value assigned to the variable.
🌐
Tech Monitor
techmonitor.ai › what is
What is Java -cp? - Tech Monitor
March 2, 2023 - The -cp, or CLASSPATH, is used as an option to the Java command.
🌐
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.
🌐
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 …
🌐
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.
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
C:> java -classpath C:\java\MyClasses utility.myapp.Cool · When the app runs, the JVM uses the class path settings to find any other classes defined in the utility.myapp package that are used by the Cool class. Note that the entire package name is specified in the command. It is not possible, for example, to set the class path so it contains C:\java\MyClasses\utility and use the command java myapp.Cool.
🌐
Coderanch
coderanch.com › t › 527426 › java › javac-cp-command
using the javac -cp command [Solved] (Beginning Java forum at Coderanch)
October 13, 2016 - If you declare a public class A then it need to be in the file A.java So you can only have 1 public class per actual file. And yes there needs to be a space in between. The correct syntax is: javac -cp .