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?.
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:
This works on Windows:
java -cp "lib/*" %MAINCLASS%
%MAINCLASS% of course is the class containing your main method.
java -cp "lib/*" -jar %MAINJAR%
%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.
Why don’t you clean up your mess by moving the REQUIRED jar files into a single folder and set the classpath to the folder? 😉