Java class loading

I want to understand how Java class loaders work. I have read several articles, but there are some things that are still not clear.

As I understand it, the first class loader is the Bootstrap (BCL) class loader. Is the JVM loading?

BCL then downloads the rt.jar library and Extension Class Loader (ECL).

In turn, the ECL loads the extensions and application class loader (ACL). The ACL is responsible for loading the entire user class from the class path.

Is this description correct?

There are a few questions:

  • Only one instance of each class loader exists in memory. (BCL, ECL, ACL)?
  • I examined the principle of delegation, but for me it is a bit unclear. How this works, let's say we need to load MyClass. The first jvm gives this ACL class name, and it is not clear to me, the ACL looks at the class path, and if there is no such class delegate, this work is to the parent OR it delegates this work to the parents right after the call, I mean the JVM gives the ACL class name, he does not look for this class, which gives its ECL, this CL, in turn, also does not do any work and does not pass it to BCL, and only if BCL cannot find this class, it returns it to the lower level (ECL) .. .. and so on. What is the right chain?
  • When we create a custom class loader, what is its parent class? ClassLoader app? Can we indicate, for example, ECL. Since the hierarchy of class loaders is not an inheritance, we specify parent in the constructor. Can we get an instance of the ECL class classloader to specify it in our user CL as the parent in the constructor.
  • Why don't classes, such as String, Object, etc., return a ClassLoadder?
+6
source share
1 answer

Answers:

  • As I understand it, the first class loader is the Bootstrap (BCL) class loader. Is the JVM loading?

    This is not explicitly defined, but will most likely be written in native code.

  • BCL then downloads the rt.jar library and Extension Class Loader (ECL).

    Yes, it will load rt.jar (prior to Java 8, since a new modular system will appear in Java 9). Whether it downloads the ECL or not is not clearly defined.

  • In turn, the ECL loads the extensions and application class loader (ACL). The ACL is responsible for loading the entire user class from the class path.

    Yes, it downloads extensions and whether it downloads an ACL or not is not explicitly defined. ACL loads class entries, really.

  • Only one instance of each class loader exists in memory? (BCL, ECL, ACL)?

    Yes this is correct. Since the class identifier is defined as a pair of FQCN and its efficient class loader, otherwise it will not work.

  • I examined the principle of delegation, but for me it is a bit unclear. How this works, let's say we need to load MyClass. The first jvm gives this ACL class name, and it is not clear to me, the ACL looks at the class path, and if there is no such class delegate, this work is to the parent OR it delegates this work to the parents right after the call, I mean the JVM gives the ACL class name, he does not look for this class, which gives its ECL, this CL, in turn, also does not do any work and does not pass it to BCL, and only if BCL cannot find this class, it returns it to the lower level (ECL) .. .. and so on. What is the right chain?

    In the standard version of Java, there is a delegation model with a parent name , which means that the class loader will first ask its parent and only then try to load the class on its own.

  • When we create a custom class loader, what is its parent class? ClassLoader app? Can we indicate, for example, ECL. Since the hierarchy of class loaders is not an inheritance, we specify parent in the constructor. Can we get an instance of the ECL class classloader to specify it in our custom CL as a parent in the constructor.

    By default, the parent of your custom class loader is the application class loader.

    The question is, why do you need this? It is unlikely that your program will behave correctly. Formally, you can do this using YourClass.class.getClassLoader().getParent() .

  • Why don't classes, such as String, Object, etc., return a ClassLoadder?

    Bootstrap bootloader is represented as null in the API.

+5
source

Source: https://habr.com/ru/post/981882/


All Articles