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

I use so many third-party libraries (jar files) that my CLASSPATH is completely confused, since I need to include a path for every jar file that I use.

I was wondering if there is a way to include all jar files in a folder using a wildcard (*) operator (e.g. * .jar). But it doesn't seem to work. Is there any other way that can shorten CLASSPATH, which currently looks like an essay;) on my PC ?.

+79
java classpath wildcard
Aug 06 '09 at 5:21
source share
5 answers

From: http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html

Class path entries can contain a wildcard for the base name * , which is considered equivalent to specifying a list of all files in a directory with the extension .jar or .JAR. For example, a class path entry of foo/* indicates all JAR files in a directory named foo. The classpath element, consisting simply of * , expands to a list of all jar files in the current directory.

This should work in Java6, not sure about Java5

(If it seems that it is not working properly, try putting quotes, for example: "foo/*" )

+83
Aug 6 '09 at 5:26
source share

This works on Windows:

 java -cp "lib/*" %MAINCLASS% 

where %MAINCLASS% , of course, is the class containing your main method.

As an alternative:

 java -cp "lib/*" -jar %MAINJAR% 

where %MAINJAR% is the jar file to run through its internal manifest.

+19
Nov 28 '12 at 16:11
source share

In Java 6, Basename basemaps were introduced; that is, "foo / *" means all the .jar files in the foo directory.

In earlier versions of Java that do not support wildcard class templates, I resorted to using the shell script shell to build the Path class by "matching" the template and distorting the results to insert the ":" characters at the appropriate points. This would be difficult to do in the file BAT ...

+5
Aug 6 '09 at 6:01
source share

If you mean that you have a CLASSPATH environment variable, I would say your mistake. I do not have such a thing on any machine with which I am developing Java. CLASSPATH is so attached to a specific project that it is impossible to have a single, correct CLASSPATH that works for everyone.

I install CLASSPATH for each project using either an IDE or Ant. I do a lot of web development, so each WAR and EAR uses its own CLASSPATH.

It is ignored by the IDE and application servers. Why do you have this? I would recommend deleting it.

0
Aug 6 '09 at 9:46
source share

Why don't you clear your clutter by moving the REQUIRED jar files to one folder and setting the path to this folder ?;)

-one
Aug 06 '09 at 5:24
source share



All Articles