views:

33182

answers:

17

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?

+25  A: 

The problem is the way the shell expands the wildcard - you want the jars to be separated by colons in the classpath specifier, but the shell is giving you spaces. If you're using Bash, you can try something like

java -classpath $( echo *.jar . | sed 's/ /:/g') my.package.Program
Adam Rosenfield
The shell is probably not expanding the wildcard at all in the original example, because it doesn't treat : specially and looks for files in the lib directory that end in ".jar:.". Since there aren't any, it passes the asterisk through to java, which doesn't expand it either.
Greg Hewgill
Thank you, that worked for iterating through the directory, and it worked when I ran a test script. However, I received this error when trying to run it as you had: customermigration.sh[4]: sed: not found. customermigration.sh: There is no process to read data written to a pipe.
Chris Serra
try 'which sed' and it will show you the full path, e.g. /bin/sed. then replace 'sed' in the script with /bin/sed or the relevant path.
Leigh Caldwell
marked down - see basszero's post
Joel
So then, this way works with ALL Java version, not just 6 - sounds like a winner...
David
+1  A: 

You need to add them all separately. Alternatively, if you really need to just specify a directory, you can unjar everything into one dir and add that to your classpath. I don't recommend this approach however as you risk bizarre problems in classpath versioning and unmanagability.

Daniel Spiewak
A: 

Think of a jar file as the root of a directory structure. Yes, you need to add them all separately.

Revah
+3  A: 

If you really need to specify all the .jar files dynamically you could use shell scripts, or Apache Ant. There's a commons project called Commons Launcher which basically lets you specify your startup script as an ant build file (if you see what I mean).

Then, you can specify something like:

<path id="base.class.path">
    <pathelement path="${resources.dir}"/>
 <fileset dir="${extensions.dir}" includes="*.jar" />
    <fileset dir="${lib.dir}" includes="*.jar"/>
</path>

In your launch build file, which will launch your application with the correct classpath.

Phill Sacre
A: 

Just a note for us non UNIX developers. The classpath requirement is the same on Windoze and the flavors of that fruity company's OS.

Tom

DIA Tom
+15  A: 

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

oxbow_lakes
+50  A: 

If you're using Java 6 (which you should be), Sun added classpath wildcards but you have to get the syntax JUST right. See Classpath. Jump down to the section, "Understanding class path wildcards" (about midway down the page)

basszero
We're actually using Java 5 at the moment. But that is good to know for the future, thanks for the tip!
Chris Serra
this saved my day.
abhin4v
Almost saved my day, except for: "However, class path wildcards are not honored in the Class-Path jar-manifest header"
Chris
A: 

Just 2 links to my own posts about declaration of jar files in manifest file and how to use ANT to automatically generate the list of jars (for using in the manifest and the -jar option for example). It might help.

A: 

The only way I know how is to do it individually, for example:

setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.

Hope that helps!

ParseTheData
+1  A: 

As previously mentioned, this can be done in Java 6. However, I've found that you need to wrap the classpath in quotes. This has worked for me in both Windows and Linux.

Linux: java -classpath "lib/*.jar:." my.package.Program

Windows: java -classpath "lib*.jar:." my.package.Program

elevine
+1  A: 

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.

interstellar
A: 

For windows quotes are required and ; should be used as separator. e.g.:

java -cp "target\*;target\dependency\*" my.package.Main

Alexey
A: 

Hi All,

I am stuck with a similar kind of problem,

My program is running fine on when I run it on eclipse,

But when I try to export it out from there as a jar and try running the jar from my command line, as java -jar MyJar.jar , I am getting an error :

One of the Jar should be in classpath.

My manifest file contains only "Main-Class: test_mail.class"and the classpath contains the location of the jar file which shows as an error as missing..

Please help me out and feel free to have any further info regarding it.

-- Praveen

Praveen
Better post this as a new question ("Ask Question" button in the top right of the page). More people will see it that way.
sth
+2  A: 

You can try java -Djava.ext.dirs=jarDirectory http://java.sun.com/j2se/1.4.2/docs/guide/extensions/spec.html

Directory for external jars when running java

Navi
This works, but watch out, pass the `-Djava.ext.dirs=` BEFORE `-jar`
Helltone
A: 

Everybody talks about setting the java classpath, but before that, it would be interesting to know what are the paths already there. And nobody talks about how to GET THIS CLASSPATH

Jean
System.out.println(System.getProperty("java.class.path"));
Wojtek