JVM - ClassLoader(类加载器)
程序员文章站
2022-07-03 14:50:33
...
ClassLoader:
在ClassLoaderSubSystem中Loading阶段加载.class文件进入内存形成Class类
一、ClassLoader的分类
package xyz.xx.chapter2;
/**
* F4 查看当前类的Hierarchy(等级图)
* Ctrl+Shift+T 搜索指定类
*/
public class ClassLosderTest {
public static void main(String[] args) {
ClassLosderTest clt = new ClassLosderTest();
// aaa@qq.com
System.out.println(clt.getClass().getClassLoader());
// aaa@qq.com
System.out.println(clt.getClass().getClassLoader().getParent());
// null(Bootstrp ClassLoader)
System.out.println(clt.getClass().getClassLoader().getParent().getParent());
// null(Bootstrp ClassLoader)
// Java核心类库都是使用“引导类加载器”加载
ClassLoader classLoader1 = String.class.getClassLoader();
System.out.println(classLoader1);
}
}
二、Java核心类库有哪些?各个加载器主要加载哪些类?
package xyz.xx.chapter2;
import sun.misc.Launcher;
import java.net.URL;
import java.util.Arrays;
/**
* F4 查看当前类的Hierarchy(等级图)
* Ctrl+Shift+T 搜索指定类
*/
public class ClassLosderTest1 {
public static void main(String[] args) {
// Bootstrp ClassLoader加载范围
// 1. jre/lib/目录下一些jar包
/*
file:/D:/Installation/jdk1.8.0_241/jre/lib/resources.jar
file:/D:/Installation/jdk1.8.0_241/jre/lib/rt.jar
file:/D:/Installation/jdk1.8.0_241/jre/lib/sunrsasign.jar
file:/D:/Installation/jdk1.8.0_241/jre/lib/jsse.jar
file:/D:/Installation/jdk1.8.0_241/jre/lib/jce.jar
file:/D:/Installation/jdk1.8.0_241/jre/lib/charsets.jar
file:/D:/Installation/jdk1.8.0_241/jre/lib/jfr.jar
file:/D:/Installation/jdk1.8.0_241/jre/classes
*/
URL[] urLs = Launcher.getBootstrapClassPath().getURLs();
for(URL i : urLs){
System.out.println(i);
}
// extClassLoader加载范围
// 1. jre/lib/ext文件夹下jar包/class文件
// 2. 系统属性java.ext.dirs对应的jar包/class文件
/*
D:\Installation\jdk1.8.0_241\jre\lib\ext
C:\Windows\Sun\Java\lib\ext
*/
String property1 = System.getProperty("java.ext.dirs");
for(String i : property1.split(";")){
System.out.println(i);
}
// AppClassLoader加载范围
// 1. java.class.path系统属性所指定的目录
// 2. 环境变量classpath中的jar包/class文件
/*
D:\Installation\jdk1.8.0_241\jre\lib\charsets.jar
D:\Installation\jdk1.8.0_241\jre\lib\deploy.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\cldrdata.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\dnsns.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\jaccess.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\jfxrt.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\localedata.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\nashorn.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\sunec.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\sunjce_provider.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\sunmscapi.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\sunpkcs11.jar
D:\Installation\jdk1.8.0_241\jre\lib\ext\zipfs.jar
D:\Installation\jdk1.8.0_241\jre\lib\javaws.jar
D:\Installation\jdk1.8.0_241\jre\lib\jce.jar
D:\Installation\jdk1.8.0_241\jre\lib\jfr.jar
D:\Installation\jdk1.8.0_241\jre\lib\jfxswt.jar
D:\Installation\jdk1.8.0_241\jre\lib\jsse.jar
D:\Installation\jdk1.8.0_241\jre\lib\management-agent.jar
D:\Installation\jdk1.8.0_241\jre\lib\plugin.jar
D:\Installation\jdk1.8.0_241\jre\lib\resources.jar
D:\Installation\jdk1.8.0_241\jre\lib\rt.jar
D:\IDEA_Projects\P001\out\production\P001
D:\Installation\ideaIC-2020.2.1.win\lib\idea_rt.jar
*/
String property = System.getProperty("java.class.path");
for(String i : property.split(";")){
System.out.println(i);
}
}
}
三、获取ClassLoader的几种方法
查看JVM系统属性清单:
for(String i : System.getProperties().toString().split(",")){ System.out.println(i); }
获取当前所在的类的类名:
System.getProperty("sun.java.command");
1> 通过Class类获取
package xyz.xx.chapter2;
/**
* F4 查看当前类的Hierarchy(等级图)
* Ctrl+Shift+T 搜索指定类
* Ctrl+Alt+o 删除不必要的包
*/
public class ClassLoaderTest2 {
// 测试获取ClassLoader的方法
public static void main(String[] args) throws ClassNotFoundException {
// 1.
// aaa@qq.com
ClassLoader classLoader = Class.forName(System.getProperty("sun.java.command")).getClassLoader();
System.out.println(classLoader);
}
}
2> 通过线程获取
package xyz.xx.chapter2;
/**
* F4 查看当前类的Hierarchy(等级图)
* Ctrl+Shift+T 搜索指定类
* Ctrl+Alt+o 删除不必要的包
*/
public class ClassLoaderTest2 {
// 测试获取ClassLoader的方法
public static void main(String[] args) throws ClassNotFoundException {
// 1.
// aaa@qq.com
ClassLoader classLoader = Class.forName(System.getProperty("sun.java.command")).getClassLoader();
System.out.println(classLoader);
}
}
上一篇: 开发中遇到的问题(持续更新)