溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java JVM-自定義類加載器

發(fā)布時間:2020-07-11 15:50:03 來源:網絡 閱讀:283 作者:wx5d21d5e6e5ab1 欄目:編程語言

自定義文件系統(tǒng)類加載器

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下的文件,轉換成字節(jié)數組
        {
            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問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI