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

Discussions

Including all the jars in a directory within the Java classpath - Studytonight
Is there a way to include all the jar files within a directory in the classpath? I'm trying java -classpath lib/*.jar:. More on studytonight.com
🌐 studytonight.com
java - How to include all the jars present at a particular directory in the CLASSPATH in one go? - Stack Overflow
I have around hundreds of jars at a particular directory which my application uses. So I thought its hard to add each jars one by one to the classpath. So is there any command or any way so that i can add all the jars at one go. some *.jar should add all the jars. ... An example would be to add "...\java... More on stackoverflow.com
🌐 stackoverflow.com
How to add Jar classpath in my dockerfile
Couple options: When initializing spark, you can add a --jars argument and point to the driver. It should be able to find it (probably better solution) in your Dockerfile, dump the file location into CLASSPATH environment variable, ENV CLASSPATH=/code/postgresql-42.2.10.jar:${CLASSPATH} My Dockerfile knowledge is a little shaky so it might not be exact, but both options would make your JDBC driver discoverable More on reddit.com
🌐 r/docker
1
3
October 23, 2021
Executable jar with an external class path reference
Hmm, not sure you can use wildcards in a manifest Class-Path, but you should be able to list the dependencies individually e.g. Class-Path: conf/dependency_one.jar conf/next_dependency.jar Kind of annoying, but should work.. More on reddit.com
🌐 r/java
17
7
November 10, 2012
🌐
W3Docs
w3docs.com › java
Including all the jars in a directory within the Java classpath | W3Docs
To include all the jars in a directory within the Java classpath, you can use the -cp or -classpath command-line option and specify the directory containing the jars.
🌐
Medium
medium.com › javarevisited › back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - ClasspathProject/ | ---> src/java/myprogram/ | ---> Main.java | ---> utils/ | ---> Util.java | ---> bin/myprogram/ | ---> Main.class | ---> utils/ | ---> Util.class · If we now move to the bin folder we can run the main class. ... Great, so why does this work? Because the default value for the classpath is the current directory.
🌐
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.
🌐
Coderanch
coderanch.com › t › 544081 › build-tools › classpath-setup-jars-multiple-directory
classpath setup by taking jars from multiple directory (Other Build Tools forum at Coderanch)
Only the commons-logging JAR is required to be in the classpath at compile time; but at runtime you would need both JARs. ... This is how I did. 1. First in build.properties I defined a variable that will list the lib dirs. lib.dirs=\ DAS/lib,\ DAS-UI/lib,\ DPS/lib,\ DSS/lib,\ DCS/lib,\ ...
🌐
Real's HowTo
rgagnon.com › javadetails › java-0587.html
Include all jars in the classpath definition - Real's Java How-to
@echo off setlocal ENABLEDELAYEDEXPANSION if defined CLASSPATH (set CLASSPATH=%CLASSPATH%;.) else (set CLASSPATH=.) FOR /R .\lib %%G IN (*.jar) DO set CLASSPATH=!CLASSPATH!;%%G Echo The Classpath definition is %CLASSPATH% ... java MyClass · According to http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html, there is a new way to include jars in a given directory without explicitly to specify each one of them.
🌐
Rip Tutorial
riptutorial.com › adding all jars in a directory to the classpath
Java Language Tutorial => Adding all JARs in a directory to the...
This tells the JVM to add all JAR and ZIP files in the someFolder directory to the classpath. This syntax can be used in a -cp argument, a CLASSPATH environment variable, or a Class-Path attribute in an executable JAR file's manifest file.See ...
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - The sh, ksh shells: Examine your .profile file for the export command. Class path entries can contain the base name wildcard character (*), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Include Jars In Java Classpath Example - Java Code Geeks
February 13, 2025 - This will include all JAR files inside the lib directory in the classpath. Make note that the wildcard approach works natively in Java 6 and later but does not support recursive directory scanning.
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
If the CLASSPATH variable is set at system startup, the place to look for it depends on your operating system: 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.
🌐
javaspring
javaspring.net › blog › including-all-the-jars-in-a-directory-within-the-java-classpath
How to Include All JAR Files in a Directory in Java Classpath: Fixing lib/*.jar Not Found Issue — javaspring.net
Go to the Libraries tab > Click Classpath > Add JARs... (or Add External JARs... if lib is outside the project). Select the lib directory > Check all .jar files > Click OK. ... Libraries > Add Library > User Library > New...
🌐
Baeldung
baeldung.com › home › java › jvm › ways to add jars to classpath in java
Ways to Add JARs to Classpath in Java | Baeldung
July 19, 2025 - Once we set the CLASSPATH, we can run our Java programs without specifying the –classpath option. It’s important to note that if we set the CLASSPATH like this, it remains valid only for that terminal session. Once the terminal is closed, the setting is lost. We can add the classpath as an environment variable to make it permanent. When we create a self-contained application, it’s helpful to bundle all the dependent JARs into one application JAR.
🌐
Coderanch
coderanch.com › t › 408016 › java › classpath-inclusion-jars-folder
classpath inclusion for all jars in a folder? (Beginning Java forum at Coderanch)
hi, i remember, that with java 5 or 6 it is possible to include a whole folder with jars. something like java -classpath libFolder/*.jar. which arguments or properties do i pass the jvm to do this? thanks. ... You don't need the .jar extension. The '*' is expanded to all jar files in the directory ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-add-jar-file-to-classpath-in-java
How to Add JAR file to Classpath in Java? - GeeksforGeeks
August 7, 2021 - We can illustrate the Java command example to add multiple JAR into classpath using Java 6 wildcard method as follows, ... In this method, we include all JAR files inside 'D:\lib' directory into the classpath. We must ensure that syntax is correct.
🌐
Stack Overflow
stackoverflow.com › q › 44075050
Java use -classpath to include multiple jars in multiple directories all in one directory? - Stack Overflow
See here for an example for specifying the classpath for the 'java' command: ... It should be the same for javac. Unfortunately you will still need to specify each directory, there is no recursive argument to add all jars in subdirectories.
🌐
Studytonight
studytonight.com › forum › including-all-the-jars-in-a-directory-within-the-java-classpath
Including all the jars in a directory within the Java classpath - Studytonight
We get around this problem by deploying a main jar file myapp.jar which contains a manifest (Manifest.mf) file specifying a classpath with the other required jars, which are then deployed alongside it. In this case, you only need to declare java -jar myapp.jar when running the code. So if you deploy the main jar into some directory, and then put the dependent jars into a lib folder beneath that, the manifest looks like: Manifest-Version: 1.0 Implementation-Title: myapp Implementation-Version: 1.0.1 Class-Path: lib/dep1.jar lib/dep2.jar NB: this is platform-independent - we can use the same jars to launch on a UNIX server or on a Windows PC.
🌐
Chilkat
chilkatsoft.com › java-classpath-Windows.asp
Java JAR Archives and classpath on Windows
javac -classpath ".;C:/chilkatJava/chilkat.jar" Test.java java -classpath ".;C:/chilkatJava/chilkat.jar" Test ... javac -Djava.class.path=.;C:/chilkatJava/chilkat.jar Test.java java -Djava.class.path=.;C:/chilkatJava/chilkat.jar Test ... IMPORTANT:On Windows systems, the semicolon character should be used for the path separator. However, on Linux systems, the colon character should be used. Privacy Statement. Copyright Chilkat Software, Inc. All rights reserved.