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
🌐
Coderanch
coderanch.com › t › 537992 › java › multiple-classpath
specify multiple classpath [Solved] (Beginning Java forum at Coderanch)
my bad, I just try javac -classpath /Users/dolphin/Library/Tomcat/lib/servlet-api.jar:/Users/dolphin/Library/Tomcat/webapps/ROOT/classes SearchSinger.java and it works! Thanks a lot !!
🌐
DEV Community
dev.to › awwsmm › easily-merge-multiple-java-classpath-arguments-1fj7
Easily Merge Multiple Java --classpath Arguments - DEV Community
June 21, 2019 - I always confuse myself as to whether the correct flags for java and the jshell are -cp or --classpath or --class-path or something else, so instead of trying to remember something like a normal human, I instead spent a few hours pulling this script together. It automatically detects any variation of cp, classpath, and class-path flags in a series of command-line arguments, and concatenates all of the correct arguments to give you a nice, clean classpath.
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.

🌐
DEV Community
dev.to › awwsmm › easily-merge-multiple-java-classpath-arguments-1fj7 › comments
[Discussion] Easily Merge Multiple Java --classpath Arguments — DEV Community
June 21, 2019 - Bash is not Java, Bash is different. Use echo for return values from function. ... Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français. ... Ph.D. in [Astroparticle] Physics ... Hi Vlastimil. I used indices because I need the i-th argument and also the i+1-th argument at the same time.
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
C:> sdkTool -classpath classpath1;classpath2... ... A command-line tool, such as java, javac, javadoc, or apt.
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › env_multiple_classpath.htm
How to set multiple classpath in Java
Following example demonstrates how to set multiple classpath. Multiple class paths are separated by a semicolon. c:> java -classpath C:\java\MyClasse1;C:\java\MyClass2 utility.testapp.main
🌐
How to do in Java
howtodoinjava.com › home › java basics › java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - Learn how to set classpath in Java ... argument. During runtime of any Java application, the CLASSPATH is a parameter that tells the JVM where to look for classes and packages. The default value of the classpath is “.” (dot), meaning that only the current directory is searched for dependencies. Specifying either the CLASSPATH environment variable or the -cp command line switch overrides this value. The order in which you specify multiple classpath ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-multiple-classpath-in-java-in-windows
How to Set Multiple Classpath in Java in Windows? - GeeksforGeeks
December 8, 2020 - If the CLASSPATH already exists in System Variables, click on the Edit button then put a semicolon (;) at the end. Paste the Path of MySQL-Connector Java.jar file.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › unix › classpath.html
2 Setting the Class Path
April 21, 2026 - java -classpath /java/MyClasses:/java/OtherClasses ... The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the previous example, the Java interpreter will ...
🌐
Medium
medium.com › @nadinCodeHat › the-world-of-java-how-java-classpath-and-jvm-arguments-work-9301d2ea21a0
The World of Java — How Java CLASSPATH and JVM Arguments work | by Nadin Pethiyagoda | Medium
July 18, 2022 - Repeat the same steps for the Application.java file in “b” directory. While inside the “b” folder we set the CLASSPATH by first setting the path to Application.jar in “a” directory and then setting for Application.jar in “b” directory. Then navigate to the home directory so that you are not in “b” directory.
🌐
Wikipedia
en.wikipedia.org › wiki › Classpath
Classpath - Wikipedia
May 30, 2026 - Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when the class is first used). The classpath tells Java where to look in the filesystem for files defining these classes.
🌐
Princeton
cs.princeton.edu › courses › archive › fall97 › cs461 › jdkdocs › tooldocs › win32 › classpath.html
classpath - Environment Variables
set CLASSPATH=C:\java\MyClasses;C:\java\OtherClasses Note that the two paths are separated by a semicolon. The order in which you specify multiple paths in the CLASSPATH variable is important. The Java interpreter will look for classes in the directories in the order they appear in the CLASSPATH ...
🌐
Blogger
javarevisited.blogspot.com › 2012 › 10 › 5-ways-to-add-multiple-jar-to-classpath-java.html
5 ways to add multiple JAR in to Classpath in Java - Examples
August 31, 2021 - From Java 1.6+ onwards you can use a wildcard to include all jars in a directory into the set classpath or provide it to the Java program directly using -classpath command-line option. Following Java, the command example shows how to add multiple JAR into classpath using Java 6 wildcard method.
🌐
Lenovo
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo US
The -cp or -classpath option allows developers to specify a custom classpath directly on the command line when compiling or running Java programs. It overrides any classpath environment variable, ensuring the provided paths are used for locating class files and resources.
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath.html
Setting the class path
To find class files in the directory /java/MyClasses as well as classes in /java/OtherClasses, you would set the class path to: % java -classpath /java/MyClasses:/java/OtherClasses ... Note that the two paths are separated by a colon. The order in which you specify multiple class path entries ...
Top answer
1 of 3
3

In addition to @Szmeby answer, if you don't know how to use file with classpath inside, you may try to create a "pathing jar".

"Pathing jar" contains only Manifest.mf file which includes next entry:

Class-Path: some.jar another.jar others.jar

You can also use wildcards to reduce length.

2 of 3
2

I think it is mainly an OS problem caused by the command line length limitation, not a java one. I had the same issue when I was playing around with jdeps, it also needed a huge classpath. Eventually I exported the classpath into a plain text file and inlined that file content as a command argument.

Assuming the name of the text file containing the classpath string is: cp.txt

Its content (partly):

/home/anon/.m2/repository/com/app/generator/2.0.jar:/home/anon/.m2/repository/com/app/model/2.0.jar:/home/anon/.m2/repository/com/generator-helpers/2.0.jar:/home/anon/.m2/repository/org/eclipse/emf/org.eclipse.emf.ecore/2.10.1-v20140901-1043/org.eclipse.emf.ecore-2.10.1-v20140901-1043.jar:/home/anon/.m2/repository/org/eclipse/emf/org.eclipse.emf.common/2.10.1-v20140901-1043/org.eclipse.emf.common-2.10.1-v20140901-1043.jar:/home/anon/.m2/repository/org/eclipse/emf/org.eclipse.emf.ecore.xmi/2.10.1-v20140901-1043/org.eclipse.emf.ecore.xmi-2.10.1-v20140901-1043.jar:/home/anon/.m2/repository/commons-io/commons-io/2.1/commons-io-2.1.jar:etc...

Then you should execute your command like this:

runProcess("java -cp $(< cp.txt) topLevelProject.com.test.project.App");

It can consume a classpath string of any size, however it is a linux-only solution. I do not know how to inline file content in a Windows command prompt. Well, at least I hope it gives you some idea to move on.