02 类加载器
分类
- 启动类加载器(Bootstrap Class Loader)、自定义加载器(非Bootstrap Class Loader,如Extension Class Loader、System Class Loader, 用户自定义等等)
- 之间关系是包含关系,非上下层、父子、继承关系
- 非java语言加载器(Bootstrap c语言)、java语言加载器
- 启动类/引导类加载器,获取不到
// 系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
// sun.misc.Launcher$AppClassLoader@18b4aac2
System.out.println(systemClassLoader);
// 获取上层,扩展加载器
ClassLoader extClassLoader = systemClassLoader.getParent();
// sun.misc.Launcher$ExtClassLoader@60e53b93
System.out.println(extClassLoader);
// 获取扩展类上层
ClassLoader bootStrap = extClassLoader.getParent();
// null
System.out.println(bootStrap);
// 用户自定义类: 默认使用系统类加载器
ClassLoader userClass = ClassLoaderTest.class.getClassLoader();
System.out.println(userClass);
// String(java核心类库)使用引导类加载器
ClassLoader stringClass = String.class.getClassLoader();
System.out.println(stringClass);
启动类加载器(引导类加载器 Bootstrap ClassLoader)
- 使用c/c++实现
- 加载java核心库(JAVA_HOME/jre/lib/rt.jar、resources.jar或sun.boot.class.path路径下内容,提供jvm自身需要的类
- 本身不继承java.lang.ClassLoader,没有父加载器
- 加载扩展类和应用类程序加载器,并指定他们的父类加载器
- 出于安全考虑,只加载包名为java、javax、sun等开头的类
扩展类加载器(Extension ClassLoader)
- Java语言编写
- 派生于ClassLoader类
- 从java.ext.dirs系统属性所指定的目录,或人JDK安装目录jre/lib/ext子目录加载,用户创建jar放入此目录,也会被加载
应用程序类加载器(系统类加载器,AppClassLoader)
- java语言编写,由sun.misc.Launcher$AppClassLoader实现
- 派生于ClassLoader类
- 负责加载环境变量classpath或系统属性java.class.path指定路径下类库
demo
显示类加载路径
System.out.println("===========BootstrapClassLoader============"); // 获取BootstrapClassLoader加载路径 URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs(); for (URL url : urls) { System.out.println(url); } ClassLoader classLoader = Provider.class.getClassLoader(); System.out.println("BootstrapClassLoader:" + classLoader); System.out.println("===========java.ext.dirs============"); // 显示扩展类加载器 String extDir = System.getProperty("java.ext.dirs"); // 分隔符不同 windows ; Linux : for (String s : extDir.split(":")) { System.out.println(s); } // 结果如下 ===========BootstrapClassLoader============ file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/resources.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/rt.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/sunrsasign.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jsse.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jce.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/charsets.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jfr.jar file:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/classes BootstrapClassLoader:null ===========java.ext.dirs============ /Users/nohi/Library/Java/Extensions /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext /Library/Java/Extensions /Network/Library/Java/Extensions /System/Library/Java/Extensions /usr/lib/java
一个类相等的前提
- 同一类加载器
- 相同类定义文件
https://github.com/thisisnohi/thinkinjava.git/...MyClassLoader