4. Application System ClassLoader

person shubham sharmafolder_openJAVAlocal_offeraccess_time November 17, 2024

The Application ClassLoader (also known as the System ClassLoader) is a class loader in Java responsible for loading classes defined by the application, such as those from the application’s classpath. It sits at the top of the Java ClassLoader hierarchy, above the Bootstrap and Extension ClassLoaders, and is the default class loader for classes in your application.

Key Characteristics of the Application (System) ClassLoader

  1. Loads Application Classes: The Application ClassLoader is tasked with loading classes from locations specified in the CLASSPATH environment variable or from paths specified via the -classpath (or -cp) option when starting the JVM.
  2. Delegates to Parent Loaders: As part of the parent delegation model, the Application ClassLoader first delegates loading requests to its parent (typically the Extension ClassLoader), which in turn delegates to the Bootstrap ClassLoader. Only if neither parent can load the class does the Application ClassLoader attempt to load it.
  3. Access via ClassLoader.getSystemClassLoader(): The Application ClassLoader can be retrieved programmatically through ClassLoader.getSystemClassLoader().
  4. Implemented in Java: Unlike the Bootstrap ClassLoader (which is written in native code), the Application ClassLoader is implemented in Java and is therefore represented as an object within the JVM.

ClassLoader Hierarchy Recap

  • Bootstrap ClassLoader: Loads core Java classes (e.g., java.lang.*, java.util.*).
  • Extension ClassLoader: Loads Java extension libraries (in Java 8 and earlier, from JAVA_HOME/lib/ext).
  • Application (System) ClassLoader: Loads classes from the application’s classpath.

In this hierarchy, the Application ClassLoader serves as the top-level loader for the application’s classes and resources, following the parent delegation model to prevent overriding of core Java and extension classes.

How the Application ClassLoader Works

  1. Classpath Setup: When the JVM is started, the Application ClassLoader looks at the CLASSPATH environment variable or the -classpath/-cp options provided. These define the directories and .jar files that contain the application’s classes.
  2. Loads User-Defined Classes: It loads all user-defined classes and application dependencies, including custom libraries, from the specified classpath.
  3. Parent Delegation: Before attempting to load a class, the Application ClassLoader first delegates to its parent (Extension ClassLoader). If the class is not found, it will then attempt to load the class itself.
  4. Default ClassLoader for Applications: Any classes loaded by Class.forName() or instances of new MyClass().getClass().getClassLoader() default to using the Application ClassLoader, as this is the standard class loader for user-defined classes.

Example of Application ClassLoader in Action

Consider a simple Java application that loads a user-defined class:

Output:

  • The AppClassLoaderExample.class.getClassLoader() will return an instance of the Application ClassLoader.
  • The String.class.getClassLoader() will return null because String is loaded by the Bootstrap ClassLoader.

Accessing the Application ClassLoader Programmatically

Java provides a static method to retrieve the Application ClassLoader:

Application ClassLoader in Java 9 and Beyond

With Java 9 and the introduction of the Java Platform Module System (JPMS):

  • The concept of modules was introduced, which allows applications to have better control over dependencies.
  • The Application ClassLoader can load modules from both the classpath and module path, enabling modularized applications.

While the CLASSPATH setup remains, modules allow applications to have a modular structure, loaded and managed by the Application ClassLoader, depending on the setup (module-path vs. classpath).

Key Advantages of the Application ClassLoader

  1. User-Defined Classes: It provides flexibility to load all application-specific classes and resources.
  2. Parent Delegation Model: By delegating to its parent class loaders, it prevents application classes from accidentally overriding core Java or extension library classes.
  3. Standard ClassLoader: As the default loader for user-defined classes, it is the most commonly used class loader in Java applications.

Summary

The Application (System) ClassLoader is the default class loader responsible for loading application-level classes from the classpath or module path. It sits at the top of the class loader hierarchy, delegating loading requests up to the Extension and Bootstrap ClassLoaders as part of the parent delegation model. This ensures a secure and structured loading of core, extension, and application classes, enabling Java applications to run smoothly with isolated and managed dependencies.

warningComments are closed.