Java - ClassLoader

// 获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
// systemClassLoader: sun.misc.Launcher$AppClassLoader@73d16e93

// 获取其上层:拓展类加载器
ClassLoader extClassLoader = systemClassLoader.getParent();
// extClassLoader: sun.misc.Launcher$ExtClassLoader@12028586

// 获取拓展类载器上层
ClassLoader bootstrapClassLoader = extClassLoader.getParent();
// bootstrapClassLoader: null  获取不到引导类加载器

//对于用户自定义类,默认使用系统类加载器进行加载
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
//classLoader: sun.misc.Launcher$AppClassLoader@73d16e93

// String 使用引导类加载器加载
ClassLoader classLoaderString = String.class.getClassLoader();
// classLoaderString: null  --> Java 核心类库使用引导类加载器加载

加载器加载路径: 

// 获取启动类加载器加载路径
URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
for (URL url : urls) {
    System.out.println(url.toExternalForm());
}

/*
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/resources.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/rt.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/sunrsasign.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/jsse.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/jce.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/charsets.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/lib/jfr.jar
file:/C:/Software/java/.jdks/jdk1.8.0_251/jre/classes
*/
        
// 获取扩展类加载器加载目录
String extDirs = System.getProperty("java.ext.dirs");
for (String s : extDirs.split(";")) {
    System.out.println(s);
}
/*
C:\Software\java\.jdks\jdk1.8.0_251\jre\lib\ext
C:\Windows\Sun\Java\lib\ext
*/

获取ClassLoader: 

// 获取当前类的ClassLoader
clazz.getClassLoader()

// 获取当前线程上下文的ClassLoader
Thread.currentThread().getContextClassLoader()

// 获取系统的ClassLoader
ClassLoader.getSystemClassLoader()

// 获取调用者的ClassLoader
DriverManager.getCallerClassLoader()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值