24

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.

Gray
  • 115,027
  • 24
  • 293
  • 354
Jan Zyka
  • 17,460
  • 16
  • 70
  • 118

3 Answers3

44

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.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    Thanks for that. One other thing I was thiking about was if I use either (probably wrongly) java.ext.dirs or specify classpath using wildcard. Is it any different from naming them one by one? Maybe the order can matter sometimes? – Jan Zyka Feb 18 '11 at 10:14
  • One point against importing 'every Jar in a directory' is that if the directory contains 1500 Jars and the app. requires only 2 of those, the JRE might have to search a great many Jars before it discovers a class or resource. I do not see the great benefit of using wildcards in class-paths (even if an app. uses 50 Jars). – Andrew Thompson Feb 18 '11 at 12:20
  • 3
    Thanks for opinion. But as I described above, my lib directory contains only the jars really needed, no more no less. I don't want to hardcode them into the script since the jar versions may changed. Would you suggest then to let maven generate the script for me with the jars being hardcoded in the script? – Jan Zyka Feb 18 '11 at 12:45
  • @AndrewThompson, That's an erroneous point. Why would you put 100 unused jars in the directory? Or if you could do that, couldn't you also specify 100 unused jars individually in the classpath? – Pacerier Aug 23 '14 at 11:02
  • @Pacerier *"Why would you put 100 unused jars in the directory?"* If 11 apps. each put 10 (unique) Jars in the directory, that's 100 unused Jars (per app.). Basic math really. – Andrew Thompson Aug 23 '14 at 11:10
  • @AndrewThompson, Why would you put jars from 11 projects all in the same directory? – Pacerier Aug 23 '14 at 15:02
  • @Pacerier I wouldn't put *any* Jars there. But all it takes is a little imagination to see that where one app. might recommend that, others might as well. – Andrew Thompson Aug 23 '14 at 15:43
  • 1
    @AndrewThompson, Apps wouldn't recommend *anything*. Apps are shipped with their own dedicated folder containing all the jars they need. Users simply double-click. Why would an app recommend putting jars into the directory of another totally unrelated app? – Pacerier Aug 23 '14 at 16:02
6

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
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • Do I get this right: If I have a library (in java.ext.dirs) that loads stuff from my main jar (that happens to be in classpath) that won't work because its on a different classloader? – Angelo Fuchs Oct 12 '14 at 08:58
4

A big problem with putting things in the lib.ext directory is that different applications may require different versions of libraries, and those may well conflict with one another.

Think of the old DLL hell from the Windows 3 days (if you remember those) when a similar situation developed when many if not most software developers placed shared libraries in the Windows/System directory because they're picked up automatically from there rather than include them with their applications and loading them explicitly.

What you want instead is to have a separate classpath for every application (so no system level classpath!), set in its startup script, and pointing to only those jar files and class directories applicable for that specific application. That way several applications with conflicting library needs can be launched side by side without interfering with each others functionality.

The same is true even more if you're a developer, where you don't want any outside libraries interfering with your application while testing it. And there's another thing: if you're developing and using the lib/ext trick (or a system level classpath), how can you ever be sure that the application you're going to ship ships with the correct libraries? If you forget to include on in your installer or installation package, you'd never notice because it's on your machine in a shared location. But the customer, who doesn't have that library, would get runtime errors and shortly be on the phone demanding support (and possibly a refund, and giving you bad reviews in the press for shipping a defective product).

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • 2
    Agree, I wrote that the application is run via a script which sets the classpath or java.ext.dirs just for the app. The lib folder (as mentioned above) is part of the app so only the jars needed for the app are there. So no shared libraries and no shared classpaths here. – Jan Zyka Feb 18 '11 at 10:47
  • 1
    @jwenting, The questioner isn't talking about putting things in the jre/lib/ext directory. He is talking about using the `java` command's flag `-Djava.ext.dirs`. – Pacerier Aug 23 '14 at 16:03