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 entryfoo/*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/*")
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 entryfoo/*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.
Videos
The java classpath wildcard expansion is unusual.
From the docs:
Understanding class path wildcards
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.
So, what you need to specify is:
java -classpath /path/to/app/conf/lib/*:/path/to/app/lib/*
If you need only specific jars, you will need to add them individually. The classpath string does not accept generic wildcards like nameJar*, *.jar, spring* etc.
Read Setting multiple jars in java classpath for more information.
seems JVM cant find the Spring related JARS. Put all required JARS in one location (say c:\libs) and use "java.ext.dirs" when runing "java" command
like :
java -Djava.ext.dirs=c:/libs com.example.ClassTest
Is there some code in Java Standard Lib that I could use instead of doing it from scratch?
No. Classpath wildcard expansion is not performed by a standard library in Java, and the expansion is performed even before any classes are loaded by the JVM. It is not a standard in the first place, and is available only if the JRE implementation supports it. As far as I know, the Oracle/Sun and OpenJDK runtimes allow for classpath wildcard expansion.
Quite obviously, you'll need to roll your own implementation if you intend to do this in Java. You can look up the implementation in C (for the OpenJDK runtime) in the wildcard.c file located in the jdk/src/share/bin directory of the sources.
AFAIK there is not.
You could probably do this fairly easily using one of the filename wildcard methods in Apache commons FilenameUtils, though you'd need to split the classpath, and then filter the wildcarded list to discard any files with the wrong suffix.
FWIW: the NIO extensions in Java 7 include support for filename wildcarding.
Use forward slash and put entire classpath in quotes:
java -cp "app.jar;dynamicLib/*" my.mainclass
When starting an application from a .jar file, the command line argument -cp is ignored. Instead, the classpath needs to be specified in the MANIFEST.MF file's "Class-Path" attribute. Unfortunately, wildcards are not supported in this attribute, so all .jar files need to be named explicitly.
Therefore, the original idea of having a "dynamicLib" directory will only work if the names of the .jar files don't change.
Alternatively you can do without the central app.jar and work with .class files instead, thus allowing you to use wildcards at the command line level.