This section provides a tutorial example on how to use ClassLoader.getSystemResources() method to return all locations of the same class file. This is useful when you are dealing with JAR hell problem.
What Is JAR Hell?
JAR hell is a class loading problem where the wrong version of a class is loaded into the JVM
from multiple JAR files.
Case 1.
When deploying the application, two JAR files, one for each version of the same class files,
are listed in the classpath, and the older version is listed first.
The System ClassLoader will load the older version of the class into JVM.
This is a deployment mistake which can be easily corrected.
Case 2.
When deploying the application, one JAR file and one class folder, one for each version of the same class files,
are listed in the classpath, and the older version is listed first.
The System ClassLoader will load the older version of the class into JVM.
This is a deployment mistake which can be easily corrected.
Case 3.
An application requires two libraries, which require different versions, two JAR files, of
third library.
If both versions of the third library use same class names,
there is no way to load both JAR files of the third library with the same ClassLoader.
One way to find how many versions of the same class are accessible in the "classpath"
is to use the ClassLoader.getSystemResources() method, which finds and returns all locations
of the give class file name.
Here is tutorial example program showing you how to find all locations of a Java class:
/**
* ClassLoaderJarHell.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
class ClassLoaderJarHell {
public static void main(String[] a) {
java.io.PrintStream out = System.out;
java.net.URL u = null;
out.println("");
out.println("Find all locations of Hello.class...");
try {
java.util.Enumeration e
= ClassLoader.getSystemResources("Hello.class");
while (e.hasMoreElements()) {
u = (java.net.URL) e.nextElement();
out.println("URL: "+u);
}
} catch(Exception e) {
e.printStackTrace();
}
out.println("");
out.println("Running Hello class...");
Hello.main(null);
}
}
Before running this tutorial example, I need to prepare two versions
of Hello class.
Version 1 of Hello class is compiled in the current folder. Hello.class is copied to MyZIP.zip:
class Hello {
public static void main(String[] a) {
System.out.println("Hello world!");
}
}
Version 2 of Hello class is compiled in a sub folder ./src. Hello.class is copied to Hello.jar:
class Hello {
public static void main(String[] a) {
System.out.println("Hello world! -- v2");
}
}
Now run ClassLoaderJarHell.java with different "classpath" settings:
>java ClassLoaderJarHell
Find all locations of Hello.class...
URL: file:/C:/herong/jvm/Hello.class
Running Hello class...
Hello world!
>java -classpath ./src/Hello.jar;.MyZIP.zip;. ClassLoaderJarHell
Find all locations of Hello.class...
URL: jar:file:/C:/herong/jvm/src/Hello.jar!/Hello.class
URL: file:/C:/herong/jvm/Hello.class
Running Hello class...
Hello world! -- v2
>java -classpath ./src/Hello.jar;MyZIP.zip;. ClassLoaderJarHell
Find all locations of Hello.class...
URL: jar:file:/C:/herong/jvm/src/Hello.jar!/Hello.class
URL: jar:file:/C:/herong/jvm/MyZIP.zip!/Hello.class
URL: file:/C:/herong/jvm/Hello.class
Running Hello class...
Hello world! -- v2
>java -classpath MyZIP.zip;./src/Hello.jar;. ClassLoaderJarHell
Find all locations of Hello.class...
URL: jar:file:/C:/herong/jvm/MyZIP.zip!/Hello.class
URL: jar:file:/C:/herong/jvm/src/Hello.jar!/Hello.class
URL: file:/C:/herong/jvm/Hello.class
Running Hello class...
Hello world!
The test result confirms that:
ClassLoader.getSystemResources("Hello.class") correctly returned all locations of
Hello.class based on the "classpath" setting.
The System ClassLoader loads the first location of the Hello.class in the JVM to run.