Difference between AppClassloader and SystemClassloader

后端 未结 3 1817
一生所求
一生所求 2021-02-02 00:16

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

相关标签:
3条回答
  • 2021-02-02 00:40

    Both AppClassLoader and SystemClassLoader are same.

    Have a look at hierarchy.

    ClassLoader follows three principles.

    Delegation principle

    Bootstrap ClassLoader is responsible for loading standard JDK class files from rt.jar and it is parent of all class loaders in Java. Bootstrap class loader don't have any parents.

    Extension ClassLoader delegates class loading request to its parent, Bootstrap and if unsuccessful, loads class form jre/lib/ext directory or any other directory pointed by java.ext.dirs system property

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

    Application class loader is a child of Extension ClassLoader and its implemented by sun.misc.Launcher$AppClassLoader class.

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

    Have a look at this blog for better understanding of these three class loaders.

    Visibility Principle

    According to visibility principle, Child ClassLoader can see class loaded by Parent ClassLoader but vice-versa is not true.

    If class Abc is loaded by Application class loader then trying to load class ABC explicitly using Extension ClassLoader will throw java.lang.ClassNotFoundException

    Uniqueness Principle

    According to this principle a class loaded by Parent should not be loaded by Child ClassLoader again

    0 讨论(0)
  • 2021-02-02 00:55

    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.

    0 讨论(0)
  • 2021-02-02 00:56

    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.

    0 讨论(0)
提交回复
热议问题