This question already has an answer here:
-
Including all the jars in a directory within the Java classpath
22 answers
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?.
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/*\"
)
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.
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 ...
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.