Application Class Loader

The application class loader loads application classes and other classes available through the application's class path or module path. In modern Java, it is commonly the system class loader returned by `ClassLoader.getSystemClassLoader()`.

Java uses class loaders to locate and define classes at runtime. The application class loader is responsible for classes supplied by the application and its dependencies, typically from the application class path or module path.

The system class loader is obtained with `ClassLoader.getSystemClassLoader()`. In common JVM configurations, that loader is the application class loader, although its concrete implementation is JVM-dependent and should not be assumed to have a particular implementation class name.

Class loading normally follows parent delegation: a class loader first gives its parent an opportunity to load a class before attempting to load it itself. For the application class loader, this commonly means the platform class loader is consulted before application locations are searched.

The application class loader is distinct from the bootstrap and platform class loaders. Bootstrap loading handles core Java runtime classes, while the platform loader handles platform classes; application code and dependencies are generally handled by the application/system loader.

Key points

Interview questions

How does the application class loader relate to class path and module path?

The application/system loader is responsible for locating application classes in the runtime's configured application class-loading environment. For traditional class-path execution, this includes class-path entries; in modular execution, application modules are associated with the module path and module system. The exact mechanics depend on whether the application is running in the class path or module system and on the JVM implementation.

What is the Application Class Loader in Java?

It is the class loader normally responsible for loading application classes and their dependencies. It commonly searches locations represented by the class path or module path. In typical JVM configurations, it is also the system class loader. Its exact implementation is JVM-dependent.

How do you obtain the application/system class loader?

You can call `ClassLoader.getSystemClassLoader()`. This returns the JVM's system class loader, which in typical configurations is the application class loader. Code should avoid depending on its concrete implementation class because that detail is not portable.

What role does parent delegation play in application class loading?

Under the usual delegation model, a class loader asks its parent to load a requested class before trying to define or locate it itself. This allows parent loaders to provide classes from higher-priority namespaces and helps avoid duplicate definitions of core classes. The application loader therefore does not simply search application locations first for every request.

Is `AppClassLoader` a portable class name for the application class loader?

No. `AppClassLoader` is an implementation detail commonly visible in OpenJDK-derived runtimes, not a Java API contract. Applications should normally use the `ClassLoader` API rather than checking for that concrete class name. The system class loader's behavior and implementation can vary between JVM implementations and configurations.