112

I have been using so many 3rd party libraries(jar files) that my CLASSPATH is completely messed up as i have to include the path for every single jar file that i use.

I've been wondering if there is a way to include all the jar files in a folder using wildcard(*) operator (like *.jar). But it seems to be not working. Is there any other way that can shorten the CLASSPATH that currently looks like an essay ;) on my PC?.

1

4 Answers 4

112

From: http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html

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.

This should work in Java6, not sure about Java5

(If it seems it does not work as expected, try putting quotes. eg: "foo/*")

Sign up to request clarification or add additional context in comments.

8 Comments

TFM says that you cannot do this in Java 1.5 or earlier.
Timesaver tip: put "*" (with surrounding double quotes) instead of just * for this to work (on Windows at least). :)
Just to extend on @Amos, for java 1.7, groovy 2.1.5 and windowsXP, I had to use -cp "./*".
The common mistake is put "foo/*.jar", that will not work. Will only work with "foo/*"
doesn't work for java 8 under windows, but does work if a backslash precedes the asterisk rather than a slash: see bugs.openjdk.java.net/browse/JDK-8131329
|
27

This works on Windows:

java -cp "lib/*" %MAINCLASS% 

where %MAINCLASS% of course is the class containing your main method.

Alternatively:

java -cp "lib/*" -jar %MAINJAR% 

where %MAINJAR% is the jar file to launch via its internal manifest.

6 Comments

I couldn't get the alternative to work. -jar is supposed to override the -cp and seems to do so even if the Class-Path is not in the jar manifest. If you want to include the working directory as well as lib this seems to work: java -cp ".\*;lib\*" %MAINCLASS%
Whole question based on the alternative not working here
Also note: Multiple wildcards may appear in a single classpath declaration, and may be combined with other non-wildcard elements, e.g, java -cp "lib/*;lib2/*;lib3/specific.jar;config/
Beware that adding jars and other files (like .class .properties .xml) to the classpath are totally different beasts. The following adds both the jar where the main class is in and some properties file that gets called during execution from the current dir on the cp java -cp .;*;"%JAVA_HOME%/jre/lib/"* com.some.package.ClassContainingMain. The '*;' part only takes care of the jars in the current dir and the '.;' part takes care of all other needed .properties files and the like
The alternative is plain wrong. When using -jar the -cp parameter is ignored.
|
5

Basename wild cards were introduced in Java 6; i.e. "foo/*" means all ".jar" files in the "foo" directory.

In earlier versions of Java that do not support wildcard classpaths, I have resorted to using a shell wrapper script to assemble a Classpath by 'globbing' a pattern and mangling the results to insert ':' characters at the appropriate points. This would be hard to do in a BAT file ...

Comments

0

If you mean that you have an environment variable named CLASSPATH, I'd say that's your mistake. I don't have such a thing on any machine with which I develop Java. CLASSPATH is so tied to a particular project that it's impossible to have a single, correct CLASSPATH that works for all.

I set CLASSPATH for each project using either an IDE or Ant. I do a lot of web development, so each WAR and EAR uses their own CLASSPATH.

It's ignored by IDEs and app servers. Why do you have it? I'd recommend deleting it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.