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, because the same class exists in multiple JAR files given in the classpath.

Case 1 - When deploying an application with two JAR files, one JAR file 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 an application with 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 a single 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) 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. This 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. This 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 different "classpath" settings:

C:\herong>java ClassLoaderJarHell
Find all locations of Hello.class...
URL: file:/C:/herong/jvm/Hello.class
Running Hello class...
Hello world!

C:\herong>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/MyZIP.zip!/Hello.class
URL: file:/C:/herong/jvm/Hello.class
Running Hello class...
Hello world! -- v2


C:\herong>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:

Last update: 2018.

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 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

 ClassChecker.java - Reports Class Loader

 ClassChecker.java - Reports Class Locations

 "superclass access check failed" Class Load Error

 Class Loading Followed by Class Initialization

 Class Class - Class Reflections

 JVM Runtime Data Areas

 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

 JRockit JVM 28.2.7 by Oracle Corporation

 Outdated Tutorials

 References

 Full Version in PDF/EPUB