14

I am so confused about these two class loaders. When talking about the hierarchy of Java class loaders, usually the bootstrap classloader and ext class loader and the third one (system classloader or app classloader) are mentioned.

To be more accurate, I checked the source code of JDK. In class Launcher, there is the code:

loader = AppClassLoader.getAppClassLoader(extcl);

In class ClassLoader, the method:

getSystemClassloader() 

Also says the system classloader is used to start the application.

So which is the third one in the hierarchy, or are the two classloaders the same?

1
  • Indeed, if you see the source code, ClassLoader#getSystemClassloader() returns scl, which is initialized in ClassLoader#getSystemClassloader() and return by Launcher#getClassLoader(). The returned loader in Launcher#getClassLoader() is AppClassLoader.getAppClassLoader(extcl). So AppClassLoader in Java servers as system class loader.
    – puppylpg
    Sep 14, 2020 at 7:54

2 Answers 2

5

System class loader is a different name for Application class loader.

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

Application class loader ... is also (confusingly) called as "system class loader" - not to be confused with bootstrap loader which loads Java "system" classes.

1
  • 2
    That is confusing as hell
    – Dustin K
    May 15, 2020 at 16:35
1

The third in the class loader hierarchy is the SystemClassloader. It is also referred as ApplicationClassloader (or AppClassLoader) at some places. This loader loads the our application code and classes found in the classpath.

Regarding the below method in the Classloader:

public static ClassLoader getSystemClassLoader()

Javadoc says:

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

The important piece here is

This is the default delegation parent for new ClassLoader instances, and is typically the class loader used to start the application

Which means, if we create our own Custom or new classloader in our application, the System or Application class loader becomes the parent for our Custom or new classloader. And calling the getSystemClassLoader() method in Custom or new Classloader returns the System(aka Application) classloader. This aligns with the java class loader delegation model as well.

And the System (aka Application) class loader is the one which has loaded our class or app from classpath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.