Jump to content

Java class loader

From Alfawiki
Java class loader
Company typeComponent
IndustrySoftware / Programming
ProductsJava class loading mechanism

The Java class loader, an integral component of the Java Runtime Environment, is responsible for dynamically loading Java class files into the Java Virtual Machine. Typically, classes are loaded lazily—the JVM only loads the necessary class files during program execution. The class loader handles file and filesystem operations on behalf of the runtime environment.

In Java, related object code is commonly bundled into libraries packaged as JAR files, which mainly consist of Java classes—distinct code units identified by name. The class loader's role is to find these libraries, extract their contents, and load classes as demanded. Each individual class can be loaded just once by a specific class loader.

Every Java class must undergo loading through a class loader. Java applications frequently depend on third-party libraries or combinations of multiple libraries.

Upon JVM initialization, three main class loaders come into play:

  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader

The bootstrap class loader, implemented in native code and embedded within the core JVM, loads essential Java libraries found in JAR files such as rt.jar located at <JAVA_HOME>/jre/lib or <JAVA_HOME>/jmods for Java versions 9 and above. This loader is not linked to any object instance; for instance, invoking returns .

The extensions class loader is tasked with loading code from extension directories like <JAVA_HOME>/jre/lib/ext or any paths specified by the java.ext.dirs system property.

The system class loader retrieves classes from locations defined by the java.class.path, which corresponds to the environment variable CLASSPATH.

User-defined class loaders

Since the Java class loader itself is written in Java, it allows developers to create custom loaders without requiring deep knowledge of JVM internals. Apart from the Bootstrap loader, each class loader has a parent set during its creation or defaults to the system class loader if none is specified.

This design enables:

  • Runtime loading and unloading of classes, including dynamic retrieval from HTTP sources, which supports:
  • Loading encrypted Java bytecode by modifying the byte stream before execution.
  • Altering bytecode dynamically at load time, useful in scenarios like aspect weaving in aspect-oriented programming.

Class loaders in Jakarta EE

Within Jakarta EE application servers, classes are loaded from deployed archives such as WAR or EAR files through a structured hierarchy of class loaders. This approach isolates applications while allowing shared access to common classes across modules. Typically, Servlet containers employ multiple class loaders to facilitate this management.

JAR hell

The term JAR hell refers to complications arising when class loading fails due to conflicts like:

  • The presence of multiple versions of a library installed simultaneously, leading to unpredictable behavior.
  • Different applications requiring incompatible versions of the same library containing identical class names, which cannot coexist within a single class loader.
  • Complex dependencies caused by nested class loaders that result in subtle bugs and difficult debugging efforts.

To combat JAR hell, the OSGi Alliance introduced a modularity framework starting with JSR 8 in 1998 applicable across Java ME, SE, and EE platforms. OSGi employs metadata within JAR manifests to control bundles that export, import, or keep packages private—thereby supporting modularity and versioned dependencies.

In 2017, Java 9 introduced the Java Platform Module System, defining a module format and repository with namespace support designed to overcome limitations inherent in traditional JAR files. Unlike OSGi, this system emphasizes backward-compatible runtime modularity but does not provide mechanisms for managing multiple library versions concurrently; thus it does not fully eliminate JAR hell.

See also

External links