类加载3

2020-11-22  本文已影响0人  得力小泡泡

MyTest类(自定义加载器)

package com.test;

import java.io.*;

public class MyTest extends ClassLoader {

    private String classLoaderName;//加载器名字

    private String path;

    private final String fileExtension = ".class";

    public MyTest(String classLoaderName)
    {
        super();// 将系统类加载器当做该类加载器的父加载器
        this.classLoaderName = classLoaderName;
    }
    public MyTest(ClassLoader parent, String classLoaderName)
    {
        super(parent);// 显示指定该类加载器的父加载器
        this.classLoaderName = classLoaderName;
    }

    public void setPath(String path)
    {
        this.path = path;
    }

    @Override
    protected Class<?> findClass(String classname) throws ClassNotFoundException {

        System.out.println("findClass invoked:" + classname);
        System.out.println("class loader name: " + this.classLoaderName);

        byte[] data = this.loadClassData(classname);

        return this.defineClass(classname, data, 0, data.length);
    }

    private byte[] loadClassData(String className)
    {
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;

        className = className.replace(".", "/");
        try {

            is = new FileInputStream(new File(this.path + className + this.fileExtension));
            baos = new ByteArrayOutputStream();

            int ch = 0;
            while(((ch = is.read()) != -1))
            {
                baos.write(ch);
            }

            data = baos.toByteArray();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                is.close();
                baos.close();
            } catch (Exception ex){
                ex.printStackTrace();
            }
        }

        return data;
    }
}

MyCat类

package com.test;

public class MyCat {

    public MyCat() {
        System.out.println("MyCat is loaded by:" + this.getClass().getClassLoader());
    }
}

MySample类

package com.test;

public class MySample {

    public MySample() {
        System.out.println("MySample is loaded by:" + this.getClass().getClassLoader());

        new MyCat();
    }
}

MyTest2类

package com.test;

public class MyTest2 {
    public static void main(String[] args) throws Exception {
        MyTest loader1 = new MyTest("loader1");
        loader1.setPath("C:/Users/小呆呆/Desktop/");//当父类加载器都解决不了的时候,才会调用自身的findClass往这个地址找
        
        Class<?> clazz = loader1.loadClass("com.test.MySample");
        System.out.println("class:" + clazz.hashCode());

        // 如果注释掉该行,那么并不会实例化MySample对象,即MySample构造方法不会被调用
        // 因此不会实例化MyCat对象,即没有对MyCat进行主动使用,这里就不会加载MyCat.class
        Object object = clazz.newInstance();
    }
}

在项目中存在MyTest.class,MyTest2.class,桌面上的com.test中存在MyCat.class和MySample.class,当实例化MySample时,初始化时会对MyCat进行实例化

输出

class:1735600054
MySample is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
findClass invoked:com.test.MySample
class loader name: loader1
class:2133927002
MySample is loaded by:com.test.MyTest@677327b6
findClass invoked:com.test.MyCat
class loader name: loader1
MyCat is loaded by:com.test.MyTest@677327b6
class:1735600054
MySample is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/MyCat
    at com.test.MySample.<init>(MySample.java:8)
    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.test.MyTest2.main(MyTest2.java:13)
Caused by: java.lang.ClassNotFoundException: com.test.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:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

输出

findClass invoked:com.test.MySample
class loader name: loader1
class:2133927002
MySample is loaded by:com.test.MyTest@677327b6
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2

命名空间的关系

image.png

理论
存在两个类


MyTest2

package com.test;

public class MyTest2 {
    public static void main(String[] args) throws Exception {
        MyTest loader1 = new MyTest("loader1");
        loader1.setPath("C:/Users/小呆呆/Desktop/");//当父类加载器都解决不了的时候,才会调用自身的findClass往这个地址找
        
        Class<?> clazz = loader1.loadClass("com.test.MySample");
        System.out.println("class:" + clazz.hashCode());

        // 如果注释掉该行,那么并不会实例化MySample对象,即MySample构造方法不会被调用
        // 因此不会实例化MyCat对象,即没有对MyCat进行主动使用,这里就不会加载MyCat.class
        Object object = clazz.newInstance();
    }
}

总结果:
项目存在MyCat.class,不存在MySample.class时
通过上面的分析可知,最终MySample类由MyTest自定义加载器进行加载,MyCat由系统类加载器进行加载,则MySample类可以访问MyCat,而MyCat不能访问MySample

MyCat类(和上面的MyCat不同)

package com.test;

public class MyCat {

    public MyCat() {
        System.out.println("MyCat is loaded by:" + this.getClass().getClassLoader());

        System.out.println("from MyCat:" + MySample.class);
    }
}

MySample类

package com.test;

public class MySample {

    public MySample() {
        System.out.println("MySample is loaded by:" + this.getClass().getClassLoader());

        new MyCat();
    }
}

输出(报错)

findClass invoked:com.test.MySample
class loader name: loader1
class:2133927002
MySample is loaded by:com.test.MyTest@677327b6
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/MySample
    at com.test.MyCat.<init>(MyCat.java:8)
    at com.test.MySample.<init>(MySample.java:8)
    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.test.MyTest2.main(MyTest2.java:13)
Caused by: java.lang.ClassNotFoundException: com.test.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:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

MyCat

public class MyCat {

    public MyCat() {
        System.out.println("MyCat is loaded by:" + this.getClass().getClassLoader());

        //System.out.println("from MyCat:" + MySample.class);
    }
}

MySample

package com.test;

public class MySample {

    public MySample() {
        System.out.println("MySample is loaded by:" + this.getClass().getClassLoader());

        new MyCat();

        System.out.println("from MySample:"  +  MyCat.class);
    }
}

输出

findClass invoked:com.test.MySample
class loader name: loader1
class:2133927002
MySample is loaded by:com.test.MyTest@677327b6
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
from MySample:class com.test.MyCat
上一篇下一篇

猜你喜欢

热点阅读