How to use a wildcard in the classpath to add multiple jars?
Solution 1
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 entryfoo/*
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/*"
)
Solution 2
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.
Solution 3
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 ...
Related videos on Youtube
paulbullard
Updated on April 04, 2020Comments
-
paulbullard about 4 years
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?.
-
Stephen C almost 15 yearsTFM says that you cannot do this in Java 1.5 or earlier.
-
Amos over 13 yearsTimesaver tip: put "*" (with surrounding double quotes) instead of just * for this to work (on Windows at least). :)
-
Kevin Sadler almost 11 yearsI 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%
-
Nick Grealy over 10 yearsJust to extend on @Amos, for java 1.7, groovy 2.1.5 and windowsXP, I had to use -cp "./*".
-
simo.3792 over 9 yearsWhole question based on the alternative not working here
-
kevinarpe over 9 yearsAlso 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/
-
DataHacker over 9 yearsBeware 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 -
odiszapc over 7 yearsIn which order .jars will be added to classpath when I use "*" ?
-
wax_lyrical about 7 yearsthe Alternative doesn't work, which was the point of the question
-
Manuel Romeiro almost 7 yearsThe common mistake is put "foo/*.jar", that will not work. Will only work with "foo/*"
-
Algiz over 6 yearsworking with java8
-
philwalk almost 6 yearsdoesn'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
-
Gary Sheppard over 5 yearsIt works for me on Windows with Java 11, using either a backslash or a slash. I don't know where this JAR wildcard trick has been all my life, but thank you.
-
a_horse_with_no_name about 5 yearsThe alternative is plain wrong. When using
-jar
the-cp
parameter is ignored.