Class loader and class loading mechanism

One o'clock eye

1 class loader is responsible for the .class file (probably on a disk, it may also be on the network) is loaded into memory for, and generate the corresponding java.lang.Class objects.

2 When the JVM starts, an initial loading class hierarchy of three class loader consisting of:

Bootstrap ClassLoader: root class loader.

Extension ClassLoader: extension class loader.

System ClassLoader: the system class loader.

3 JVM class loading mechanism mainly has the following three mechanisms:

Overall responsibility: overall responsibility for the so-called, that is to say when a class loader is responsible for loading a Class of the Class depends and other Class will also be referenced class loader is responsible for loading, unless explicitly use another class loader you load.

Parent delegate: the so-called father of the commission is to let parent (parent) class loader tries to load the Class, only in the parent class loader when the class tries to load the class can not be loaded from your own class path.

Caching: caching mechanism will ensure that all had been loaded Class will be cached, when the program requires the use of a Class, class loader cache search for the Class start, only when the Class object does not exist in the cache, the system will read the binary data corresponding to the class, and converts it into Class object and stored in cache. That's why we changed the Class, you must restart the program JVM reason, will modify the program to take effect.

Two combat

Code 1

import java.net.*;

public class BootstrapTest
{
   public static void main(String[] args)
   {
      // 获取根类加载器所加载的全部URL数组
      URL[] urls = sun.misc.Launcher.
            getBootstrapClassPath().getURLs();
      // 遍历、输出根类加载器加载的全部URL
      for (int i = 0; i < urls.length; i++)
      {
         System.out.println(urls[i].toExternalForm());
      }
   }
}

2 runs

file:/D:/Program/Java/jdk1.8.0_162/jre/lib/resources.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/lib/rt.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/lib/sunrsasign.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/lib/jsse.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/lib/jce.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/lib/charsets.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/lib/jfr.jar
file:/D:/Program/Java/jdk1.8.0_162/jre/classes

Three combat

Code 1

import java.util.*;
import java.net.*;
import java.io.*;

public class ClassLoaderPropTest
{
   public static void main(String[] args)
         throws IOException
   {
      // 获取系统类加载器
      ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
      System.out.println("系统类加载器:" + systemLoader);
      /*
      获取系统类加载器的加载路径——通常由CLASSPATH环境变量指定
      如果操作系统没有指定CLASSPATH环境变量,默认以当前路径作为
      系统类加载器的加载路径
      */
      Enumeration<URL> em1 = systemLoader.getResources("");
      while(em1.hasMoreElements())
      {
         System.out.println(em1.nextElement());
      }
      // 获取系统类加载器的父类加载器:得到扩展类加载器
      ClassLoader extensionLader = systemLoader.getParent();
      System.out.println("扩展类加载器:" + extensionLader);
      System.out.println("扩展类加载器的加载路径:"
            + System.getProperty("java.ext.dirs"));
      System.out.println("扩展类加载器的parent: "
            + extensionLader.getParent());
   }
}

2 runs

系统类加载器:sun.misc.Launcher$AppClassLoader@18b4aac2
file:/E:/Java/IDEA_Java/out/production/IDEA_Java/
扩展类加载器:sun.misc.Launcher$ExtClassLoader@14ae5a5
扩展类加载器的加载路径:D:\Program\Java\jdk1.8.0_162\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
扩展类加载器的parent: null

 

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/94960425