JVM中有四种类加载器,启动类加载器,扩展类加载器,应用程序类加载器,用户自定义加载器(以JDK8为例)
1.启动类加载器
加载的jre,jre/lib文件下的文件
URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
for (URL url : urls) {
System.out.println(url);
}
/*
输出
file:/E:/Mysoftware/jdk8/jre/lib/resources.jar
file:/E:/Mysoftware/jdk8/jre/lib/rt.jar
file:/E:/Mysoftware/jdk8/jre/lib/sunrsasign.jar
file:/E:/Mysoftware/jdk8/jre/lib/jsse.jar
file:/E:/Mysoftware/jdk8/jre/lib/jce.jar
file:/E:/Mysoftware/jdk8/jre/lib/charsets.jar
file:/E:/Mysoftware/jdk8/jre/lib/jfr.jar
file:/E:/Mysoftware/jdk8/jre/classes
*/
2.扩展类加载器
加载jre/lib/ext/下的jar包
URL[] urls = ((URLClassLoader) ClassLoader.getSystemClassLoader().getParent()).getURLs();
for (URL url : urls) {
System.out.println(url);
}
/*
file:/E:/Mysoftware/jdk8/jre/lib/ext/access-bridge-64.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/cldrdata.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/dnsns.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/jaccess.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/jfxrt.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/localedata.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/nashorn.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/sunec.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/sunjce_provider.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/sunmscapi.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/sunpkcs11.jar
file:/E:/Mysoftware/jdk8/jre/lib/ext/zipfs.jar
*/
3.应用程序类加载器
就是加载自己编写的程序,也就是classpath下的文件。
自己在java.lang包下定义一个String类,
package java.lang;
/**
* @Author: MaiYu
* @Date: Create in 19:31 2020/12/12
*/
public class String {
static {
System.out.println("这是自定义字符串");
}
public static void main(String[] args) {
System.out.println("测试" );
}
}
/*
错误: 在类 java.lang.String 中找不到 main 方法, 请将 main 方法定义为:
public static void main(String[] args)
否则 JavaFX 应用程序类必须扩展javafx.application.Application
它会首先去启动类加载器去定义,
*/
好处:
1.一个类不会被重复加载,相同的类文件被不同的类加载器加载产生的是两个不同的类。
2.避免核心类被篡改,不能创建于核心类全限定名的类
1.自己定义的String类,没有用,不会被创建
自定义加载器的话,需要继承 ClassLoader 。如果我们不想打破双亲委派模型,就重写 ClassLoader 类中的 findClass() 方法即可,无法被父类加载器加载的类最终会通过这个方法被加载。但是,如果想打破双亲委派模型则需要重写 loadClass() 方法
没有打破双亲委派机制;
重写了findClass方法
package github.write.funding2;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @Author: MaiYu
* @Date: Create in 21:23 2020/12/15
*/
public class TestStaticBlock {
static{
System.out.println("static block init");
}
public static void main(String[] args) {
Class<?> class0=TestStaticBlock.class;
try {
Class<?> class1= class0.getClassLoader().loadClass("github.write.funding2.TestStaticBlock");
ClassLoader classLoader = new MyClassLoader();
Class<?> class2 = classLoader.loadClass("github.write.funding2.TestStaticBlock");
System.out.println(class1.hashCode());
System.out.println(class2.hashCode());
System.out.println(class1.equals(class2));
}catch (Exception e){
e.printStackTrace();
}
}
public static class MyClassLoader extends ClassLoader{
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String myPath="F:/GitClone/funding2/target/classes/github/write/funding2/TestStaticBlock"+".class";
System.out.println(myPath);
byte[] classBytes=null;
FileInputStream in=null;
try {
File file = new File(myPath);
in=new FileInputStream(file);
classBytes=new byte[(int)file.length()];
in.read(classBytes);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Class<?> clazz=defineClass(name,classBytes,0,classBytes.length);
return clazz;
}
}
}
/*
static block init
2003749087
2003749087
true
*/
解释:虽然自定义累加器,但是没有打破双亲委派机制,所以是由同一个类加载器进行加载。所以是同一个类
如果想要打破双亲委派机制,需要重写loadclass方法。