Why is there three kinds of classloader, in java? what is the purpose of this design?

Why do you use three kinds of classloader, in java? what is the purpose of this design? And why is the parental appointment mechanism used in this way? I have seen a lot of principles, but I have never understood the benefits of doing so.

Mar.16,2021

classloader the top one is bootstrapclassloader . As for why the model of parental delegation mechanism is designed, it is simply to protect the security of JDK . If you write a System class, if you do not have this mechanism, then the class you write will be loaded, so the invasion of JDK is very large, so now there is such a mechanism. bootstrapclassloader will go to the rt package to see if there is a System class. If so, then load the JDK class directly, and will not deal with the newly defined class. The designer designs such a pattern to protect the integrity and consistency of JDK .


  1. BootstrapClassLoader : can only be used to load the JDK core library, and the system variable is the class under sun.boot.class.path . This directory under the% JAVA_HOME%/jre/lib/ resources.jar;rt.jar and other core class libraries, the underlying loader is written in CPP, of course, you can not call it.
  2. ExtClassLoader : used to load some extension classes, with the system variable being the class in java.ext.dirs . Function: load the developer's own extension class.
  3. AppClassLoader : used to load the user class, which is the class under java.class.path , that is, the class we wrote ourselves.

Why are these three loaders designed in this way in the order of BootstrapClassLoader > ExtClassLoader > AppClassLoader,? Mainly for expansion and security. First of all, you use BootstrapClassLoader as a core class loader, loading only the core classes and not coupling with others. And why these three loaders should be designed should be put together with the parent delegation mechanism.

what is parental delegation mechanism: to put it simply, when you need to load a class, you must load it from the top-level parent loader first, and if the parent loader cannot, give it to the child loader. It is equivalent to asking the father what to do when a child has to make a decision.

Why should there be such a parental appointment mechanism:

  1. guarantee uniqueness: just imagine, if there is no parent delegation model but is loaded by each class loader, if users write a class of the same name java.lang.Object and put it in ClassPath, multiple class loaders load this class into memory, and multiple different Object classes will appear in the system, so the comparison results between classes and the uniqueness of classes will not be guaranteed.
  2. ensure security: since all user classes will first check whether there are such resources through bootstrapclassloader , or load them directly in Anhui Province, thus ensuring that the underlying classes must be pre-loaded, which can ensure the security of the virtual machine.
Menu