Class Loading Problem - JAR Hell

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, HerongYang.com, All Rights Reserved.
 */
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 ./src/Hello.jar:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world! -- v2"); 	
   }
}

Now run ClassLoaderJarHell.java with using different "classpath" settings with JDK 1.7.0:

>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 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:

Comparing to the same test with JDK 1.6.0 in 2010, the behavior of class searching is not changed.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

ClassLoader Class - Class Loaders

 What Is Class Loader?

 What Is java.lang.ClassLoader Class?

 Accessing the ClassLoader of a Class

 JVM "-verbose:class" Option

 loadClass() Method - Loading Classes Explicitly

 getSystemResource() Method - Finding Files

Class Loading Problem - JAR Hell

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 Outdated Tutorials

 References

 PDF Printing Version