通过类名发挥这个类的Class实例
根据给定的字节码流 b,off 和 len 参数表示实际的 class 信息在byte 数组中的位置和长度,其中 byte 数组 b是 classloader 从外部获取的
查看一个类
启动类加载器负责加载系统的核心类(rt.jar的java类),扩展类加载器加载 %JAVA_HOME/lib/ext/*.jar中的类,应用类加载器用于加载用户类 (classpath),自定义类加载器加载一些特殊路径的类(自定义classloader)
jar -cvf test.jar HelloLoader.class 把class打包成jar
例子:在 ext 路径下放一个自己 jar 包并加载
package com.mousycoder.server; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
idea 通过 structs->artifacts->jar 然后 build-> build artifacts->build 生成 helloworld.jar
放到 /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext
package com.mousycoder.mycode.thinking_in_jvm; import java.lang.reflect.Method; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-06 10:35 */ public class ExtClassLoader { public static void main(String[] args) throws ClassNotFoundException { System.out.println(System.getProperty("java.ext.dirs")); Class<?> helloClass = Class.forName("com.mousycoder.server.HelloWorld"); System.out.println(helloClass.getClassLoader()); } }
输出
/Users/mousycoder/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java sun.misc.Launcher$ExtClassLoader@610455d6
可以看出是 ExtClassLoader 加载 java.ext.dirs 目录
package com.mousycoder.mycode.thinking_in_jvm; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-06 11:13 */ public class MyClassLoader extends ClassLoader { private final static Path DEFAULT_CLASS_PATH = Paths.get("","/Users/mousycoder/My"); private final Path classDir; public MyClassLoader(){ super(); this.classDir = DEFAULT_CLASS_PATH; } public MyClassLoader(String classDir){ super(); this.classDir = Paths.get(classDir); } public MyClassLoader(String classDir, ClassLoader parent){ super(parent); this.classDir = Paths.get(classDir); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { try { byte[] classBytes = this.readClassBytes(name); if (null == classBytes || 0 == classBytes.length){ throw new ClassNotFoundException("can not load the class" + name); } return this.defineClass(name,classBytes,0,classBytes.length); } catch (IOException e){ e.printStackTrace(); } return null; } private byte[] readClassBytes(String name) throws ClassNotFoundException, IOException { String classPath = name.replace(".","/"); Path classFullPath = classDir.resolve( "HelloWorld1.class"); if (!classFullPath.toFile().exists()){ throw new ClassNotFoundException("The class" + name + "mpt found"); } try (ByteArrayOutputStream baos = new ByteArrayOutputStream()){ Files.copy(classFullPath,baos); return baos.toByteArray(); } catch (IOException e){ throw new ClassNotFoundException("load the class " + name + "occur error",e); } } @Override public String toString() { return "My ClassLoader"; } } package com.mousycoder.mycode.thinking_in_jvm; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-06 11:34 */ public class MyClassLoaderTest { public static void main(String[] args) throws ClassNotFoundException { MyClassLoader classLoader = new MyClassLoader(); Class<?> class1 = classLoader.loadClass("com.mousycoder.mycode.thinking_in_jvm.HelloWorld1"); System.out.println(class1.getClassLoader()); } } package com.mousycoder.mycode.thinking_in_jvm; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-06 11:46 */ public class HelloWorld1 { public static void main(String[] args) { System.out.println("Hello world1 "); } }
把helloword1变成class放到/Users/mousycoder/My目录下即可
输出 My ClassLoader 代表 自定义类加载器加载了该类
打破双亲委托机制,让上层父类加载器可以使用子类的加载器加载对象,比如Spi中的接口类在系统加载器中,但是实现类在应用加载器中