Search code examples
javapropertiesjarclasspath

CLASSPATH vs java.ext.dirs


Is there any reason why to favor using (possibly very long) CLASSPATH variable to set which jars should be on classpath durign application run then to use the java 1.5+ property -Djava.ext.dirs which specifies whole directory(directories) of jars to be searched?

To make it real-life example I have standalone java application with lib folder containing all dependent jars. So far the start script is setting all the (maybe 20) jars to CLASSPATH variable one by one. Since now my application archive is generated by Maven I can't see in advance what the jar names would be (e.g. I change version of a JAR). Of course I can go through the lib dir in the startup script and add all jars found there to the CLASSPATH variable again. Or probably make maven to generate this script for me.

Questions: Would it be OK and appropriate to replace all of this by simply setting the java.ext.dirs property to contain what it contains + my extra lib dir in my script? Any caveats hidden there?

Thanks for replies.


Solution

  • java.ext.dirs has a very specific use: it's used to specify where the extension mechanism loads classes from. Its used to add functionality to the JRE or to other libraries (such as JAI). It's not meant as a general-purpose class-loading mechanism.

    Use the wildcard character * instead. It was introduced in Java 6, so many people still don't know it's possible.