类加载器
类加载器的作用
类加载器(class loader)用来加载Java类到Java 虚拟机中。
一般来说,Java虚拟机使用Java类的方式如下:
- Java源程序(.java 文件)在经过Java编译器编译之后就被转换成Java字节代码(.class 文件)。
- 类加载器负责读取Java字节代码,并转换成java.lang.Class类的一个实例。
每个这样的实例用来表示一个 Java 类。通过此实例的 newInstance()方法就可以创建出该类的一个对象。实际的情况可能更加复杂,比如 Java 字节代码可能是通过工具动态生成的,也可能是通过网络下载的。
JVM默认的加载器
- Bootstrap(引导类加载器):它用来加载Java的核心库,是用原生代码来实现的,并不继承自java.lang.ClassLoader。
- ExtClassLoader(扩展类加载器):它用来加载Java的扩展库。Java虚拟机的实现会提供一个扩展库目录。该类加载器在此目录里面查找并加载Java类。
- AppClassLoader(系统类加载器):它根据Java应用的类路径(CLASSPATH)来加载 Java 类。一般来说,Java应用的类都是由它来完成加载的。可以通过ClassLoader.getSystemClassLoader()来获取它。
类加载器也是一个Java类,本身也需要其他类加载器加载,必须要一个不是Java类的类加载器加载,这个类加载器就是Bootstrap(根结点)。
类加载器采用父子关系的树结构进行组织,在实例化每个类加载器时都需要指定其父级类加载器或者采用JVM默认的类加载器。
类加载树如图所示:
代码验证
public class Main {
public static void main(String[] args) {
ClassLoader classLoader=Main.class.getClassLoader();
while (null!=classLoader){
System.out.println(classLoader.getClass().getName());
classLoader=classLoader.getParent();
}
}
}
output:
sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$ExtClassLoader
注释:class Main位于buildpath目录下,所以类加载器为AppClassLoader,AppClassLoader的父级加载器为ExtClassLoader,ExtClassLoader的父级加载器为Bootstrap,但是Bootstrap不是一个Java类,所以输出为null
Class ClassLoader
类名:
public abstract class ClassLoaderextends Object
JDK的定义
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
Every Class object contains a reference to the ClassLoader that defined it.
简单来说就是:
- 基本上所有的类加载器都是 java.lang.ClassLoader类的一个实例
- ClassLoader根据一个指定的类的名称,找到或者生成其对应的字节代码,然后从这些字节代码中定义出一个Java类,即java.lang.Class类的一个实例。
- 除此之外,ClassLoader还负责加载Java应用所需的资源,如图像文件和配置文件等。
ClassLoader中与加载类相关的部分方法:
以上方法来源于JDK8
方法 | 英文描述 |
---|---|
ClassLoader getParent() | Returns the parent class loader for delegation. |
Class<?> findClass(String name) | Finds a class with the specified binary name, loading it if necessary. |
Class<?> loadClass(String name) | Loads the class with the specified binary name. |
Class<?> defineClass(String name, byte[] b, int off, int len) | Converts an array of bytes into an instance of class Class. |
void resolveClass(Class<?> c) | Links the specified class. |
注意:binary name为字节码文件名
类加载机制
首先类加载,是把class文件从硬盘读取到内存中。
类加载方式:
- 程序在运行过程中当遇到通过new等方式生成对象时,隐式调用类装载器加载对应的类到jvm中
- 通过class.forname()等方法,显式加载需要的类
- 通过ClassLoader.loadClass()方法动态加载
类加载的步骤:
装载:查找和导入class文件;
-
连接:
(1)检查:检查载入的class文件数据的正确性; (2)准备:为类的静态变量分配存储空间; (3)解析:将符号引用转换成直接引用(这一步是可选的)
初始化:初始化静态变量,静态代码块。
类加载器的委托机制:
-
当JVM需要加载一个类时,到底选择哪个类加载器进行加载了?
1)首先当前线程的类加器会去加载线程中的第一个类。 2)如果类A引用了类B,JVM会使用加载A的类加载器加载类B。 3)当然也可以调用直接Class<?> loadClass(String name)来制定某个类加载器去加载类
当类加载器加载类时,会委托给父级类加载器加载,当所有的祖宗类加载器没有加载到类,才会调用发起者类加载器,如果还是加载不了就会抛出ClassNotFoundException。
Java类的动态加载:
添加JVM option
-verbose:class
public class ClassDynamicLoading {
public static void main(String[] args) {
System.out.println("休息");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new ClassA().print("ClassA第一次使用");
System.out.println("休息");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new ClassB().print("ClassB第一次使用");
new ClassA().print("ClassA第二次使用");
}
}
class ClassA{
static {
System.out.println("static code");
}
public void print(String str){
System.out.println(str);
}
}
class ClassB{
public void print(String str){
System.out.println(str);
}
}
output:
休息
[Loaded ClassA from file:/Users/wangkui/Desktop/Java%e7%b1%bb%e5%8a%a0%e8%bd%bd%e6%9c%ba%e5%88%b6/_classloader/out/production/_classloader/]
static code
ClassA第一次使用
休息
[Loaded ClassB from file:/Users/wangkui/Desktop/Java%e7%b1%bb%e5%8a%a0%e8%bd%bd%e6%9c%ba%e5%88%b6/_classloader/out/production/_classloader/]
ClassB第一次使用
ClassA第二次使用
分析以上代码输出可知,类只会使用到时才会被加载,加载后再次使用不会被加载。静态代码块只会在类加载完成后执行一次。
参考文档
深入探讨 Java 类加载器
JDK 8.0