class :1735600054 Exception in thread "main" java.lang.NoClassDefFoundError: com/twodragonlake/jvm/classloader/MyCat MySample is loaded by : sun.misc.Launcher$AppClassLoader@18b4aac2 at com.twodragonlake.jvm.classloader.MySample.<init>(MySample.java:6) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at com.twodragonlake.jvm.classloader.MyTest17_1.main(MyTest17_1.java:11) Caused by: java.lang.ClassNotFoundException: com.twodragonlake.jvm.classloader.MyCat at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more
findClass invoked com.twodragonlake.jvm.classloader.MySample this.classLoaderName : loader1 class :2133927002 MySample is loaded by : com.twodragonlake.jvm.classloader.MyTest16@677327b6 MyCat is loaded by : sun.misc.Launcher$AppClassLoader@18b4aac2
之后【class :2133927002】是MyTest17_1的打印,在MySample的构造器new MyCat的时候,加载器了MySample的加载器(MyTest16)会尝试加载MyCat,MyTest16自己并不会去加载MyCat,它首先会委托应用类加载器去加载,,应用类加载器能不能加载呢?答案是可以加载,因为MyCat在当前classPath下(当前工程里边的,不是【E:\data\classes\】下边的),所以后边不会出现自定义加载MyTest16的log日志,之后直接打印MyCat构造器的输出【MyCat is loaded by : sun.misc.Launcher$AppClassLoader@18b4aac2】。MySample和MyCat是由2个不同的加载器加载出来的。
ok,继续下一个实验:我们在MyCat 构造器里边加入一行代码:
1 2 3 4 5 6
public class MyCat { public MyCat(){ System.out.println("MyCat is loaded by : "+this.getClass().getClassLoader()); System.out.println("from MyCat : "+MySample.class);//加入打印MySample的一行代码 } }
findClass invoked com.twodragonlake.jvm.classloader.MySample this.classLoaderName : loader1 Exception in thread "main" java.lang.NoClassDefFoundError: com/twodragonlake/jvm/classloader/MySample at com.twodragonlake.jvm.classloader.MyCat.<init>(MyCat.java:6) at com.twodragonlake.jvm.classloader.MySample.<init>(MySample.java:6) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at com.twodragonlake.jvm.classloader.MyTest17_1.main(MyTest17_1.java:11) Caused by: java.lang.ClassNotFoundException: com.twodragonlake.jvm.classloader.MySample at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 8 more class :2133927002 MySample is loaded by : com.twodragonlake.jvm.classloader.MyTest16@677327b6 MyCat is loaded by : sun.misc.Launcher$AppClassLoader@18b4aac2
MySample is loaded by : com.twodragonlake.jvm.classloader.MyTest16@677327b6 【MySample由MyTest16加载】 MyCat is loaded by : sun.misc.Launcher$AppClassLoader@18b4aac2 【MyCat由AppClassLoader加载】
public class MySample { public MySample(){ System.out.println("MySample is loaded by : "+this.getClass().getClassLoader()); new MyCat(); System.out.println("form MySample :"+MyCat.class); } }
MyCat (注释掉MySample的打印):
1 2 3 4 5 6
public class MyCat { public MyCat(){ System.out.println("MyCat is loaded by : "+this.getClass().getClassLoader()); // System.out.println("from MyCat : "+MySample.class); } }
findClass invoked com.twodragonlake.jvm.classloader.MySample this.classLoaderName : loader1 class :2133927002 MySample is loaded by : com.twodragonlake.jvm.classloader.MyTest16@677327b6 MyCat is loaded by : sun.misc.Launcher$AppClassLoader@18b4aac2 form MySample :class com.twodragonlake.jvm.classloader.MyCat