The Extension ClassLoader in Java is a class loader responsible for loading additional classes and libraries that extend the core Java runtime. It sits between the Bootstrap ClassLoader and the Application (System) ClassLoader in the class-loading hierarchy, and it specifically loads classes from Java’s extension libraries.
Key Characteristics of the Extension ClassLoader
- Location of Extension Libraries: The Extension ClassLoader loads classes from a designated directory within the Java runtime environment, usually
JAVA_HOME/lib/ext
. This is known as the extension directory. - Parent Delegation: It follows the parent delegation model and is a child of the Bootstrap ClassLoader. This means it first delegates to the Bootstrap ClassLoader to load any core classes, and if the Bootstrap ClassLoader cannot find the requested class, the Extension ClassLoader attempts to load it from the extension libraries.
- Loads Optional Libraries: It allows Java developers to add optional libraries that expand the JVM’s functionality, such as cryptography libraries, advanced I/O libraries, or even custom modules that extend core Java APIs.
- Customizable via Java Properties: While Java 9 and above deprecated the extension mechanism, you can still customize the loading of libraries using Java properties.
ClassLoader Hierarchy
The Extension ClassLoader is part of the hierarchy:
- Bootstrap ClassLoader: Loads core Java classes (
java.lang
,java.util
, etc.). - Extension ClassLoader: Loads classes from the extension library path,
JAVA_HOME/lib/ext
. - Application (System) ClassLoader: Loads classes from the application’s classpath.
Each class loader can delegate to its parent in this hierarchy before attempting to load classes on its own.
How Extension ClassLoader Works
- Load from Extension Directories: The Extension ClassLoader is designed to load classes and resources from the designated extension directories. In Java 8 and earlier, this is typically
JAVA_HOME/lib/ext
. - Configuration: In Java 8 and earlier versions, you can specify additional extension directories using the
java.ext.dirs
system property. For example:
1java -Djava.ext.dirs=/path/to/extension_dir MyAppThis command adds
/path/to/extension_dir
to the list of directories where the Extension ClassLoader searches for classes. - Library Separation: Using this mechanism, developers can isolate certain libraries from application-level classes, ensuring these libraries are loaded once and shared across multiple applications.
Example of Extension ClassLoader in Action
Suppose you add a custom .jar
file named custom-extension.jar
to the JAVA_HOME/lib/ext
directory in Java 8. This .jar
file contains a class named CustomLibrary
.
1 2 3 4 5 |
public class ExtensionLoaderExample { public static void main(String[] args) { System.out.println(CustomLibrary.class.getClassLoader()); // Should output Extension ClassLoader } } |
In this case:
CustomLibrary
will be loaded by the Extension ClassLoader.- If you run the program and check the class loader for
CustomLibrary
, you will see the Extension ClassLoader as the responsible class loader.
Extension ClassLoader in Java 9 and Later
With Java 9 and the introduction of the Java Platform Module System (JPMS), the concept of the extension class loader has largely been replaced by the modularization of the JDK itself. In Java 9 and beyond:
- The
lib/ext
directory is no longer used for extensions. - Optional and additional libraries can now be organized as modules or added to the application’s module path.
The Extension ClassLoader still exists, but the lib/ext
directory is no longer the designated location for extension libraries.
Why the Extension ClassLoader Matters
- Shared Libraries: It allows certain libraries to be shared across multiple applications without being bundled individually in each application’s classpath.
- Isolation: The Extension ClassLoader enables separating core Java classes from optional libraries, which may not be required by all applications.
- Parent Delegation: It participates in the Java parent delegation model, preventing lower-level classes from accidentally overriding core or extended classes.
Summary
The Extension ClassLoader in Java loads optional or additional libraries from a specified directory (JAVA_HOME/lib/ext
in Java 8 and earlier) as part of the JVM’s class-loading hierarchy. While largely deprecated with Java 9’s modular system, it remains an important part of the historical class-loading mechanism in Java.