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

Answer from basszero on Stack Overflow
🌐
HowToDoInJava
howtodoinjava.com › home › java examples › java – set classpath from command line
Java - Set Classpath from Command Line
January 25, 2022 - In Windows, the path separator is semicolon ( ; ) and the path separator in Linux is colon ( : ). ... Use -classpath or -cp option to provide the classpath locations while starting the Java application or tool.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › unix › classpath.html
2 Setting the Class Path
April 21, 2026 - For example, suppose you want the ... that it contains /java/MyClasses. To run that application, you could use the following java command: java -classpath /java/MyClasses utility.myapp.Cool...
🌐
Blogger
javarevisited.blogspot.com › 2011 › 01 › how-classpath-work-in-java.html
How to Set Classpath for Java on Windows and Linux? Steps and Example
Classpath in Java is the path to the directory or list of the directory which is used by ClassLoaders to find and load classes in the Java program. Classpath can be specified using CLASSPATH environment variable which is case insensitive, -cp or -classpath command-line option or Class-Path attribute in manifest.mf file inside the JAR file in Java.
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.

🌐
How to do in Java
howtodoinjava.com › home › java basics › java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - ... There is a subtle but serious ... all classes and not just for dependant classes. The first statement “$ javac –classpath C:\dependency\framework.jar MyApp.Java” compiles fine and places the MyApp.class file in the current ...
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
For example, suppose you want the ... class path so that it contains C:\java\MyClasses. To run that app, you could use the following JVM command: C:> java -classpath C:\java\MyClasses utility.myapp.Cool...
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › classpath.html
Setting the Classpath in Java
If the CLASSPATH variable does not exist, select New. Type CLASSPATH for the variable name and .;C:\introcs for the variable value. Click OK three times. ... Use your favorite text editor (e.g., TextEdit) to add the following line to the file /Users/username/.bashrc.
🌐
Mcmaster
cas.mcmaster.ca › ~lis3 › javatutorial › Classpath.htm
java tutorial, CLASSPATH, -classpath, -d
If the necessary class files are ... set, java or javac will try to look for the necessary class files into the directories of $CLASSPATH. setenv CLASSPATH ~/jjava/class<cr> -- this command is for tcsh....
Find elsewhere
🌐
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 - However, if we take a closer look at the Java man page on Linux, it says the classpath separator is the semicolon (;). For example, the man page of the java command from the latest(ver.17) OpenJDK shows:
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › paths.html
PATH and CLASSPATH (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The CLASSPATH variable is one way ... class path or the extensions directory.) The preferred way to specify the class path is by using the -cp command line switch....
🌐
Medium
medium.com › javarevisited › back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - However, it is required by the java command to run. Let’s compile this little project. First we create a bin folder to hold the compiled stuff in the root project folder then compile the two files [2]. > mkdir bin > javac src/java/myprogram/Main.java src/java/myprogram/utils/Util.java -d bin/ You should now have the following file structure. ClasspathProject/ | ---> src/java/myprogram/ | ---> Main.java | ---> utils/ | ---> Util.java | ---> bin/myprogram/ | ---> Main.class | ---> utils/ | ---> Util.class
🌐
Edureka
edureka.co › blog › set-java-classpath
How To Set Classpath In Java | Java Path And Classpath | Edureka
June 19, 2023 - It commonly contains paths to Jar files with OS path separator in windows it is ‘;’. If you want to specify classpath while running any program using command line you can use Java -cp “some.jar;someOther.jar” com.edureka.HelloWorld .
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-classpath-in-java
How to Set Classpath in Java? - GeeksforGeeks
July 23, 2025 - Example: set CLASSPATH=.;C:\Users\GFG\JavaClasses;C:\Program Files\Java\libs · 1. Select Start · 2. Go to the Control Panel · 3. Select System and Security · 4. Select Advanced System settings · 5. Click on Environment Variables · 6. Click ...
🌐
Wikipedia
en.wikipedia.org › wiki › Classpath
Classpath - Wikipedia
May 30, 2026 - Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath-linux.html
Setting the class path
For example, suppose you want the ... path so that it contains /java/MyClasses. To run that app, you could use the following JVM command: % java -classpath /java/MyClasses utility.myapp.Cool...
🌐
Dkessner
dkessner.github.io › ProcessingLibraryExamples › classpath.html
Setting the Java CLASSPATH | ProcessingLibraryExamples
export CLASSPATH=".:/Applications/Processing.app/Contents/Java/core/library/*" If you are using an IDE, you will want to set the CLASSPATH in the project or application settings. If you are running your programs from the command line, you will want to include the above export line in a script (e.g.
🌐
UCSB
sites.cs.ucsb.edu › ~cappello › 50-2005-Winter › lectures › classpath.html
Setting the class path
For example, suppose you want the ... path so that it contains /java/MyClasses. To run that app, you could use the following JVM command: % java -classpath /java/MyClasses utility.myapp.Cool...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
How to set Classpath in Java - Java Code Geeks
May 10, 2021 - Run the required command(SDK tool eg: java, javac) with -classpath added · Example: 1.
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-to-set-a-classpath-in-java
Different Ways to Set a Classpath in Java - GeeksforGeeks
July 23, 2025 - Enter Variable name :classpath [Don't give space between class path] Variable value:<directory_location>(for example in my F:\workspace\bin) Click OK->OK->OK. Close all windows, open a new command prompt, and run the java command