ClassLoader的分类
Java中的ClassLoader有三种:Bootstrap ClassLoader 、Extension ClassLoader、App ClassLoader。以下是jdk1.8的类加载器
加载路径
package com.example.demo; import java.net.URL; import java.net.URLClassLoader; public class Test { public static void main(String[] args) { System.out.println("BootstrapClassLoader 的加载路径: "); //String[] bootUrls = System.getProperty("sun.boot.class.path").split(";"); URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs(); for (URL url : urls) System.out.println(url); System.out.println("----------------------------"); //取得扩展类加载器 URLClassLoader extClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader().getParent(); System.out.println(extClassLoader); System.out.println("扩展类加载器 的加载路径: "); urls = extClassLoader.getURLs(); for (URL url : urls) System.out.println(url); System.out.println("----------------------------"); //取得应用(系统)类加载器 URLClassLoader appClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); System.out.println(appClassLoader); System.out.println("应用(系统)类加载器 的加载路径: "); urls = appClassLoader.getURLs(); for (URL url : urls) System.out.println(url); System.out.println("----------------------------"); } }
运行结果如下:
BootstrapClassLoader 的加载路径:
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/resources.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/rt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/sunrsasign.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jsse.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jce.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/charsets.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jfr.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/classes
----------------------------
sun.misc.Launcher$ExtClassLoader@776ec8df
扩展类加载器 的加载路径:
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/access-bridge-64.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/cldrdata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/dnsns.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jaccess.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jfxrt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/localedata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/nashorn.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunec.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunjce_provider.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunmscapi.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunpkcs11.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/zipfs.jar
----------------------------
sun.misc.Launcher$AppClassLoader@18b4aac2
应用(系统)类加载器 的加载路径:
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/charsets.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/deploy.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/access-bridge-64.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/cldrdata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/dnsns.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jaccess.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jfxrt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/localedata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/nashorn.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunec.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunjce_provider.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunmscapi.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunpkcs11.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/zipfs.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/javaws.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jce.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jfr.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jfxswt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jsse.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/management-agent.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/plugin.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/resources.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/rt.jar
file:/F:/myproject/demo/target/classes/
自定义类加载器
package com.example.demo; public class Test { public static void main(String[] args) throws ClassNotFoundException { String classPath = "F:/"; CustomerClassLoader myClassLoader = new CustomerClassLoader(classPath); String packageNamePath = "LogTest"; Class logTest = myClassLoader.loadClass(packageNamePath); System.out.println("classloader is :" + logTest.getClassLoader().getClass().getName()); } }
package com.example.demo; import java.io.FileInputStream; public class CustomerClassLoader extends ClassLoader { private String loadPath; public CustomerClassLoader(String loadPath) { this.loadPath = loadPath; } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { try { byte[] bytes = loadByte(name); return defineClass(name, bytes, 0, bytes.length); } catch (Exception e) { System.out.println(e.getMessage()); } return null; } private byte[] loadByte(String className) throws Exception { className = className.replaceAll("\\.", "/"); FileInputStream fileInputStream = new FileInputStream(loadPath + "/" + className + ".class"); int available = fileInputStream.available(); byte[] data = new byte[available]; fileInputStream.read(data); fileInputStream.close(); return data; } }