102

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

paulbullard
  • 1,107
  • 2
  • 8
  • 8
  • There could be an issues with wildcard in classpath on Windows: http://stackoverflow.com/questions/11607873/escape-wildcard-processing-in-application-arguments – Mike Jul 23 '12 at 08:35

4 Answers4

100

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

gturri
  • 10,843
  • 9
  • 35
  • 53
Itay Maman
  • 28,289
  • 9
  • 76
  • 114
25

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.

Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
Chris Milburn
  • 770
  • 1
  • 10
  • 20
  • 8
    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%` – Kevin Sadler Jul 15 '13 at 15:12
  • 2
    Whole question based on the alternative not working [here](http://stackoverflow.com/questions/250166/noclassdeffounderror-while-trying-to-run-my-jar-with-java-exe-jar-whats-wron) – simo.3792 Oct 09 '14 at 04:52
  • 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/` – kevinarpe Nov 05 '14 at 08:07
  • 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 – DataHacker Dec 16 '14 at 14:35
  • the Alternative doesn't work, which was the point of the question – wax_lyrical May 11 '17 at 10:20
  • 1
    The alternative is plain wrong. When using `-jar` the `-cp` parameter is ignored. – a_horse_with_no_name May 07 '19 at 08:19
6

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

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
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.

duffymo
  • 293,097
  • 41
  • 348
  • 541