You use either -jar or -cp, you can't combine the two. If you want to put additional JARs on the classpath then you should either put them in the main JAR's manifest and then use java -jar or you put the full classpath (including the main JAR and its dependencies) in -cp and name the main class explicitly on the command line

java -cp 'MyProgram.jar:libs/*' main.Main

(I'm using the dir/* syntax that tells the java command to add all .jar files from a particular directory to the classpath. Note that the * must be protected from expansion by the shell, which is why I've used single quotes.)

You mention that you're using Ant so for the alternative manifest approach, you can use ant's <manifestclasspath> task after copying the dependencies but before building the JAR.

<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar">
  <classpath>
    <fileset dir="libs" includes="*.jar" />
  </classpath>
</manifestclasspath>

<jar destfile="MyProgram.jar" basedir="classes">
  <manifest>
    <attribute name="Main-Class" value="main.Main" />
    <attribute name="Class-Path" value="${myprogram.manifest.classpath}" />
  </manifest>
</jar>

With this in place, java -jar MyProgram.jar will work correctly, and will include all the libs JAR files on the classpath as well.

Answer from Ian Roberts on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › deployment › jar › downman.html
Adding Classes to the JAR File's Classpath (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
By using the Class-Path attribute in your application's JAR file manifest, you can avoid having to specify a long -classpath flag when launching java to run your application.
🌐
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 - It’s important to note that the ... of the Java installation is a legacy mechanism, where JAR files placed in it are automatically added to the classpath....
🌐
Medium
medium.com › javarevisited › back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - The classpath is simply a list of directories, JAR files, and ZIP archives to search for class files [1]. The runtime needs to know where to find your compiled classes so that’s what you’re providing here.
🌐
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 - This is the preferred option if you are passing –classpath option while running your Java program as java –classpath ${CLASSPATH} Main. Here CLASSPATH shell variable can contain a list of Jar files required by your application.
Top answer
1 of 4
178

You use either -jar or -cp, you can't combine the two. If you want to put additional JARs on the classpath then you should either put them in the main JAR's manifest and then use java -jar or you put the full classpath (including the main JAR and its dependencies) in -cp and name the main class explicitly on the command line

java -cp 'MyProgram.jar:libs/*' main.Main

(I'm using the dir/* syntax that tells the java command to add all .jar files from a particular directory to the classpath. Note that the * must be protected from expansion by the shell, which is why I've used single quotes.)

You mention that you're using Ant so for the alternative manifest approach, you can use ant's <manifestclasspath> task after copying the dependencies but before building the JAR.

<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar">
  <classpath>
    <fileset dir="libs" includes="*.jar" />
  </classpath>
</manifestclasspath>

<jar destfile="MyProgram.jar" basedir="classes">
  <manifest>
    <attribute name="Main-Class" value="main.Main" />
    <attribute name="Class-Path" value="${myprogram.manifest.classpath}" />
  </manifest>
</jar>

With this in place, java -jar MyProgram.jar will work correctly, and will include all the libs JAR files on the classpath as well.

2 of 4
30

When the -jar option is used the -cp option is ignored. The only way to set the classpath is using manifest file in the jar.

It is easier to just use the -cp option, add your jar file to that, then explicitly call the main class.

Also, assuming the /home/user/java/MyProgram/jar/libs folder contains jar files (as opposed to class files) this won't work. You cannot specify a folder of jar file but must specify each jar file individually in the classpath (it is worth writing a simple shell script to do this for you if there are a significant number of jars).

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 - During application runtime, application ... To set CLASSPATH environment variable, find the location of user environment variables in your machine and add all paths where Jar files are stored....
Find elsewhere
🌐
Chilkat
chilkatsoft.com › java-classpath-Windows.asp
Java JAR Archives and classpath on Windows
Copy the JAR to one of the directories listed in the CLASSPATH environment variable.
🌐
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 - This option is viable when we are passing -classpath option while running our java program like java -classpath $(CLASSPATH) Main. In this case, CLASSPATH shell variable contains the list of Jar file which is required by the application. One of the best advantages of using classpath command-line option is that it allows us to use every application to have its own set of JAR classpath.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Include Jars In Java Classpath Example - Java Code Geeks
February 13, 2025 - You can specify JAR files on the command line using the -cp (or -classpath) option when executing a Java program. This method is useful when running Java programs manually without using an Integrated Development Environment (IDE) or build tools.
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this: C:> java -classpath C:\java\MyClasses\myclasses.jar ...
🌐
Wikipedia
en.wikipedia.org › wiki › Classpath
Classpath - Wikipedia
May 30, 2026 - When overriding however, it is advised to include the current folder "." into the classpath in the case when loading classes from current folder is desired. The same applies not only to java launcher but also to javac, the java compiler. If a program uses a supporting library enclosed in a Jar file ...
🌐
Apache Maven
maven.apache.org › shared › maven-archiver › examples › classpath.html
Set Up The Classpath – Apache Maven Archiver
December 20, 2025 - The manifest classpath produced using the above configuration would look like this: Class-Path: WEB-INF/lib/org/codehaus/plexus/plexus-utils-1.1.jar WEB-INF/lib/commons-lang/commons-lang-2.1.jar
🌐
Chilkat
chilkatsoft.com › java-classpath-Linux.asp
Java JAR Archives and classpath on Linux
Copy the JAR to one of the directories listed in the CLASSPATH environment variable.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - When classes are stored in an archive file (a zip or JAR file) the class path entry is the path to and including the zip or JAR file. For example, the command to use a class library that is in a JAR file as follows: java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
🌐
Baeldung
baeldung.com › home › java › core java › understanding java’s classpath vs. build path
Understanding Java’s Classpath vs. Build Path | Baeldung
August 28, 2024 - The classpath is an environment variable used by the Java Virtual Machine (JVM) to locate and load classes when running a Java program. It specifies a list of directories, JAR files, and ZIP files where the JVM should look to find and load class ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-jar-file-to-classpath-in-java
How to set classpath when class files are in .jar file using Java
July 21, 2023 - Following example demonstrates how to set class path when classes are stored in a .jar or .zip file. c:> java -classpath C:\java\myclasses.jar utility.testapp.main