學習反射章節-獲取類載入器及其類載入路徑
阿新 • • 發佈:2022-05-18
public class 類載入器 { public static void main(String[] args) throws IOException { //獲取系統類載入器 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); System.out.println(systemClassLoader); //獲取拓展類載入器 ClassLoader extensionClassLoader = systemClassLoader.getParent(); System.out.println(extensionClassLoader); System.out.println(ClassLoader.getPlatformClassLoader()); //獲取根類載入器,結果無法獲取,為null //因為根類載入器沒有繼承ClassLoader抽象類,是C++實現的,所以拓展類載入器的getParent()返回null. System.out.println(extensionClassLoader.getParent()); //獲取系統類載入器的類載入路徑 Enumeration<URL> enumeration = systemClassLoader.getResources(""); while (enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } //獲取拓展類載入器的類載入路徑 System.out.println(System.getProperty("java.ext.dirs")); } }
執行結果如下:
jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc
jdk.internal.loader.ClassLoaders$PlatformClassLoader@7c30a502
jdk.internal.loader.ClassLoaders$PlatformClassLoader@7c30a502
null
file:/E:/study/JavaSE/out/production/JavaSE/
null