How to use a wildcard in the classpath to add multiple jars?

119,517

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 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/*")

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

Share:
119,517

Related videos on Youtube

paulbullard
Author by

paulbullard

Updated on April 04, 2020

Comments

  • paulbullard
    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
    Stephen C almost 15 years
    TFM says that you cannot do this in Java 1.5 or earlier.
  • Amos
    Amos over 13 years
    Timesaver tip: put "*" (with surrounding double quotes) instead of just * for this to work (on Windows at least). :)
  • Kevin Sadler
    Kevin Sadler almost 11 years
    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%
  • Nick Grealy
    Nick Grealy over 10 years
    Just to extend on @Amos, for java 1.7, groovy 2.1.5 and windowsXP, I had to use -cp "./*".
  • simo.3792
    simo.3792 over 9 years
    Whole question based on the alternative not working here
  • kevinarpe
    kevinarpe over 9 years
    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/
  • DataHacker
    DataHacker over 9 years
    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
  • odiszapc
    odiszapc over 7 years
    In which order .jars will be added to classpath when I use "*" ?
  • wax_lyrical
    wax_lyrical about 7 years
    the Alternative doesn't work, which was the point of the question
  • Manuel Romeiro
    Manuel Romeiro almost 7 years
    The common mistake is put "foo/*.jar", that will not work. Will only work with "foo/*"
  • Algiz
    Algiz over 6 years
    working with java8
  • philwalk
    philwalk almost 6 years
    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
  • Gary Sheppard
    Gary Sheppard over 5 years
    It 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
    a_horse_with_no_name about 5 years
    The alternative is plain wrong. When using -jar the -cp parameter is ignored.