Cover source: Wechat public account chaos – koala

Influenced by various circumstances, I began to look at the KNOWLEDGE of the JVM again.

1, Java is too much volume, can not go to the deep learning.

2, the interview questions asked many, forced to learn.

3. Pure curiosity. Like a sentence: “eight hours for life, eight hours for development.” — I hope that when I meet you on farewell Day, you will have achieved something. ‘

An overview,

The Java Classloader (English: Java Classloader) is a part of the Java Runtime Environment that is responsible for dynamically loading Java classes into the memory space of the Java virtual machine. Classes are usually loaded on demand, that is, the first time the class is used. Thanks to class loaders, the Java runtime system does not need to know about files and file systems.

The ClassLoader is only responsible for loading the class file, and it is up to the Execution Engine to decide whether it can run.

== Each Java class must be loaded into memory by some class loader. = =

Here is:

The loaded class information is stored in a memory space called the method area. In addition to the Class information, the method area also holds runtime constant pool information, possibly including literal string and numeric constants. (This constant information is a memory map of the constant pool part of the Class file.)

Second, class loader classification

The JVM supports two types of classloaders. Conceptually, a user-defined ClassLoader refers to a class of classloaders Defined by the developer in a program. However, the Java Virtual Machine specification does not define this, and instead divides all classloaders derived from an abstract ClassLoader into custom classloaders

Class loader structure diagram:

Bootstrap ClassLoader (root ClassLoader)

Bootstrap ClassLoader== Bootstrap ClassLoader== Bootstrap ClassLoader

  • Such loaders are part of the Java virtual machine and are written in native code (C++). So it’s not retrieves

  • Load the core Java class libraries in the /jre/lib directory or the directory specified by the -xbootclasspath parameter.

  • As shown in figure:

  • As shown above, to load the Java core library, rt. This jar jar package root is the Bootstrap class loader is responsible for loading, which contains the Java class of various core Java. Lang, Java. IO, Java. Util, Java. SQL, etc.,

  • The Bootstrap ClassLoader does not inherit from java.lang.ClassLoader and does not have a parent loader

  • Load extension classes and application class loaders and act as their parent class loaders

  • Bootstrap Starts the class loader to load only package namesJava, Javax, sunAnd so on

Code tests:

public class BootstrapClassLoaderTest {
    public static void main(String[] args) {
        String s = new String();
        ClassLoader loader = s.getClass().getClassLoader();
        System.out.println("Let's see if String is loaded by ==>"+loader);
        //Bootstrap ClassLoader ==>null
        Integer integer = 2;
        ClassLoader classLoader1 = integer.getClass().getClassLoader();
        System.out.println("Let's see if Integer is loaded."+classLoader1);
        //Bootstrap ClassLoader ==>null}}Copy the code

We can’t get the BootstrapClassLoader, which is written in C++.

Test summary:

We can prove that classes starting with Java, Javax, Sun, and so on are indeed loaded by the bootstrap class loader. Everyone else can have a try.

You might also think, when we build a project, we create a java.lang package and write a String class or a custom class (MyUser) underneath it. Does starting the classloader load it?

Answer: what is ???? See below.

Extension ClassLoader is an Extension ClassLoader.

  • A derived class of ClassLoader
  • The parent ClassLoader is the Bootstrap ClassLoader.
  • Use to load Java’s extension libraries in the directories specified in

    /jre/lib/ext, or java.ext.dirs.
  • The Implementation of the Java Virtual Machine provides a directory of extension libraries. The class loader looks for and loads Java classes in this directory.
  • This class is implemented by sun.misc.Launcher$ExtClassLoader.
  • Java language writing

The environment requires JDK 8. My environment is JDK11. So under the code from blog.csdn.net/sj158149630… Moved in.

Code tests:

public class ClassLoaderTest1 {
    public static void main(String[] args) {

        System.out.println("********** starts the class loader **************");
        // Get the path of the API that the BootstrapClassLoader can load
        URL[] urLs = sun.misc.Launcher.getBootstrapClassPath().getURLs();
        for (URL element : urLs) {
            System.out.println(element.toExternalForm());
        }
        // Select a class from the path above and see what its classloader is: bootstrap classloader
        ClassLoader classLoader = Provider.class.getClassLoader();
        System.out.println(classLoader); //null

        System.out.println("*********** extends the classloader *************");
        String extDirs = System.getProperty("java.ext.dirs");
        for (String path : extDirs.split(";")) {
            System.out.println(path);
        }

        // Select a class from the path above and see what its classloader is: extension classloader
        ClassLoader classLoader1 = CurveDB.class.getClassLoader();
        System.out.println(classLoader1);//sun.misc.Launcher$ExtClassLoader@1540e19d}}Copy the code

System.out.println(classLoader); // Null again proves that we cannot get the bootstrap class loader

********** Starts the class loader ************** File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/resources. The jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/rt. The jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/sunrsasign jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/jsse jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/jce jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/charsets. The jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/JFR jar File: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/classes null * * * * * * * * * * * the extension class loader * * * * * * * * * * * * * / Users/xiexu/Library/Java/Extensions: / Library/Java/JavaVirtualMachines jdk1.8.0 _241. JDK/Contents/Home/jre/lib/ext: / Libra ry/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java sun.misc.Launcher$ExtClassLoader@d716361Copy the code

AppClassLoader (application class loader, system class loader)

  • Also written in the Java language.
  • By the sun. The misc. LaunchersAppClassLoader implementation behind code (view)
  • Derived from the ClassLoader class
  • The class loader is the default class loader in the programIn general, Java application classes are loaded by it
  • Through this. GetSystemclassLoader ()Method can get the class loader
  • Loads the class library specified on the user path (ClassPath)

Code tests:

public class AppClassLoaderTest {
    public static void main(String[] args) {
        BootstrapClassLoaderTest loaderTest = new BootstrapClassLoaderTest();
        ClassLoader classLoader = loaderTest.getClass().getClassLoader();
        System.out.println(classLoader);
        //jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc }}Copy the code

== Summary == : So in general, Java application classes are loaded by it

User-defined ClassLoader

You can implement your own ClassLoader by inheriting the Java.lang. ClassLoader class to meet specific needs without having to fully understand the Java Virtual Machine’s classloading details.

Can be used to:

  • Classes are loaded or unloaded at runtime. This is often used in:

    • Implementation scripting language
    • For bean generators
    • Allows user-defined extensibility
    • Allows communication between namespaces. This is the basis of the CORBA/RMI protocol.
  • Changes to Java bytecode loading, for example, can be used for cryptographic loading of Java class bytecode.

  • Modify the loaded bytecode weavingof aspects when usingaspect-oriented Programming).

2.5. About ClassLoader Classes

==ClassLoader class, which is an abstract class, after which all classloaders inherit from ClassLoader (excluding the startup ClassLoader) ==

How do I get a ClassLoader?

There are four ways:

Obtain the Classloader of the current class

Get the ClassLoader for the current thread context

3. Obtain the ClassLoader of the system

4, get the caller’s this DriverManager. GetCallerClassLoader ()

public static void main(String[] args) {
    //1. Obtain the ClassLoader for the current class
    BootstrapClassLoaderTest classLoaderTest = new BootstrapClassLoaderTest();
    ClassLoader classLoader = classLoaderTest.getClass().getClassLoader();
    System.out.println(classLoader);
    //2. Get the ClassLoader for the current thread context
    new Thread(){
        @Override
        public void run(a) {
            System.out.println(Thread.currentThread());
            ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
            System.out.println(classLoader);
        }
    }.run();
    //3. Obtain the ClassLoader of the system
    ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    System.out.println(systemClassLoader);

}
Copy the code

2.6 Loading flow chart

The following figure is from

Fourth, the answer

I also personally to try, all are wrong. Report a packet error directly. The same goes for creating a String.

Code and structure:

Error: this Java package already exists in another module. The boot class loader cannot be found.

self-talk

Everyday anxiety life, let a person very tired, but helpless.