温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java JVM-自定义类加载器

发布时间:2020-07-11 15:50:03 来源:网络 阅读:272 作者:wx5d21d5e6e5ab1 栏目:编程语言

自定义文件系统类加载器

public class Loader extends ClassLoader{
    private String rootDir;

public Loader(String rootDir)
{
    this.rootDir=rootDir;
}

//重写父类方法
protected Class<?> findClass(String name) throws ClassNotFoundException{

    Class<?> c=findLoadedClass(name); //查找已经被加载的类,返回Class类的实例

    //不为空则返回已经加载过的类
    if(null!=c)
    {
        return c;
    }else  //如果没有类,让父类去加载
    {
        ClassLoader parent =this.getParent();
        try {
        c=parent.loadClass(name);   //委派父类加载
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        if(c!=null)
        {
            return c;
        }else   //如果还没获取,则读取d:/myjava/cn/sxt/in/User.class下的文件,转换成字节数组
        {
            byte[] classData=getClassData(name);
            if(classData==null)
            {
                throw new ClassNotFoundException(); //如果没加载到,手动抛出异常
            }else
            {
                c=defineClass(name,classData,0,classData.length);
            }
        }
    }
    return c;
}

    private byte[] getClassData(String classname)
{
    String path=rootDir+"/"+classname.replace('.', '/')+".class";
    ByteArrayOutputStream bos=null;
    InputStream is=null;
    try {
        is=new FileInputStream(path);
        bos=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int len=-1;
        while((len=is.read(buffer))!=-1)
        {
            bos.write(buffer,0,len);
        }
        bos.flush();
        return bos.toByteArray();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }finally {
        try { if(null!=is)
            {
             is.close();
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
         try
         {
             if(null!=bos)
             {
                 bos.close();
             }
         }catch(IOException e)
         {
             e.printStackTrace();
         }

    }
    return null;
}
}

加载:

public class Tt {

public static void main(String[] args) throws Exception {

    Loader loader=new Loader("d:myjva");
    Loader loader2=new Loader("d:myjva");
    Class<?> c=loader.loadClass("cn.sxt.in.HelloWorld");
    Class<?> c2=loader.loadClass("cn.sxt.in.HelloWorld");
    Class<?> c3=loader2.loadClass("cn.sxt.in.HelloWorld");
    Class<?> c4=loader2.loadClass("java.lang.String");
    Class<?> c5=loader2.loadClass("cn.sxt.in.helloworld"); //调用项目下的文件
    //加载器不一样加载相同类会被JVM认为是不同类
    System.out.println(c.hashCode());
    System.out.println(c2.hashCode());
    System.out.println(c3.hashCode());
    System.out.println(c4.hashCode());
    System.out.println(c3.getClassLoader());  //返回自定义的加载器
    System.out.println(c4.getClassLoader()); //用的引导类加载器,返回null
    System.out.println(c5.getClassLoader()); //返回的是应用类加载器
}
}

cmd:编译java文件

C:\Users\10853>d:

D:\>cd myjava

D:\myjava>javac -d . HelloWorld.java

D:\myjava>java cn.sxt.in.HelloWorld
aa

D:\myjava>
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI