Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLASSPATH vs java.ext.dirs

Tags:

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.

like image 761
Jan Zyka Avatar asked Feb 18 '11 09:02

Jan Zyka


People also ask

What is Java Ext Dirs?

ext. dirs referred to a single directory, but as of Java 6 it is a list of directories (like CLASSPATH) that specifies the locations in which extensions are searched for. The first element of the path is always the lib/ext directory of the JRE. The second element is a directory outside of the JRE.

Is Java classpath recursive?

Just remember that classpath never works recursively, so if you want to scan a subdirectory for jars you need to point it explicitly.


2 Answers

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.

like image 52
Joachim Sauer Avatar answered Sep 20 '22 15:09

Joachim Sauer


Joachim made a good point about the wildcard character shortcut. But since the question is asking about differences and caveats to watch out for...

One difference is that if we specify our libraries under the -Djava.ext.dirs flag, they will be loaded using the extension classloader (e.g. sun.misc.Launcher.ExtClassLoader) instead of the system classloader (e.g. sun.misc.Launcher.AppClassLoader).

Assuming that in our library, we have a class named Lib. And our application runs this code:

public class Main {     public static void main(String args[]) {         System.out.println(System.getProperty("java.ext.dirs"));         ClassLoader test_cl = Main.class.getClassLoader();         ClassLoader lib_cl = Lib.class.getClassLoader();         System.out.println(test_cl == lib_cl);         System.out.println(test_cl);         System.out.println(lib_cl);     } } 

The console output will be:

C:\Program Files\Java\jdk1.6.0\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext true sun.misc.Launcher$AppClassLoader@107077e sun.misc.Launcher$AppClassLoader@107077e 

when the application is run using the command java -cp "folder/*;." Main.

However, when the application is run using the command java -Djava.ext.dirs=folder Main, then the output will instead be:

folder false sun.misc.Launcher$AppClassLoader@107077e sun.misc.Launcher$ExtClassLoader@7ced01 
like image 44
Pacerier Avatar answered Sep 19 '22 15:09

Pacerier