Difference between AppClassloader and SystemClassloader

I got so confused in these two class loaders when it comes to the java class loader hierarchy, bootstrap class loader and ext class loader are usually loaded, and the third one (system class loader or class loader), to be more precise, I checked the source jdk code, in the Launcher class, there is a code:

loader = AppClassLoader.getAppClassLoader(extcl); 

in the ClassLoader class, the method: getSystemClassloader () also says that the system class loader is used to start the application.

what is the third in the hierarchy, or are the two classloaders the same?

+6
source share
3 answers

Both AppClassLoader and SystemClassLoader same.

Look at the hierarchy.

ClassLoader follows three principles.

Delegation principle

ClassLoader hieraechy

Bootstrap ClassLoader is responsible for loading the standard JDK class files from rt.jar and is the parent of all class loaders in Java. The bootloader of the Bootstrap class has no parents.

Extension ClassLoader delegates the class loading request to its parent element, Bootstrap, and if it fails, loads the jre / lib / ext class directory or any other directory specified by the java.ext.dirs system property

System or Application class loader , and it is responsible for loading specific application classes from the CLASSPATH, -classpath or -cp command line environment variable, the Class-Path attribute of the manifest file inside the JAR.

The application class loader is a child of the Extension ClassLoader and is implemented by the sun.misc.Launcher$AppClassLoader .

Except Bootstrap class loader , which is implemented in the native language, mainly in C, all Java class loaders implemented using java.lang.ClassLoader .

Take a look at the blog for a better understanding of these three class loaders.

Visibility principle

In accordance with the principle of visibility, Child ClassLoader can see the class loaded by Parent ClassLoader but vice-versa is not true .

If the Abc class is loaded using the Application class loader , than trying to load the ABC class explicitly using the Extension ClassLoader , it will throw java.lang.ClassNotFoundException

Principle of uniqueness

In accordance with this principle, the class loaded by the parent should not be loaded again by Child ClassLoader

+6
source

The third in the class loader hierarchy is SystemClassloader. In some places, it is also called ApplicationClassloader (or AppClassLoader). This loader loads our application code and classes found in the class path.

Regarding the method below in Classloader:

public static ClassLoader getSystemClassLoader ()

Javadok says:

Returns the system class loader for delegation. This is the default parent value for new instances of ClassLoader and is typically the class loader used to launch the application.

Important part here

This is the default parent delegate for new instances of ClassLoader and is usually the class loader used to launch the application.

This means that if we create our own Custom or new class loader in our application, the System or Application class loader will become the parent for our Custom or new classloader . And a call to the getSystemClassLoader () method in a User or new Classloader returns the System (aka Application) class loader. This also matches the delegation model of the java class loader.

And the System (aka Application) class loader is the one that loaded our class or application from the class path.

+1
source

The system class loader is another name for the application class loader.

Source: https://blogs.oracle.com/sundararajan/entry/understanding_java_class_loading

The application class loader ... is also (confused) called the "system class" loader "- not to be confused with the boot loader, which loads the Java" system "classes.

+1
source

All Articles