Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Including all the jars in a directory within the Java classpath

Is there a way to include all the jar files within a directory in the classpath?

I'm trying java -classpath lib/*.jar:. my.package.Program and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to the classpath separately?
by

4 Answers

Kajalsi45d
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
MounikaDasa
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.
akshay1995
My solution on Ubuntu 10.04 using java-sun 1.6.0_24 having all jars in "lib" directory:

java -cp .:lib/ my.main.Class

If this fails, the following command should work (prints out all *.jars in lib directory to the classpath param)

java -cp $(for i in lib/
.jar ; do echo -n $i: ; done). my.main.Class
RoliMishra
For me, this works in windows.

java -cp "/lib/;" sample

For linux

java -cp "/lib/:" sample

Login / Signup to Answer the Question.