您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java的ClassLoader機制是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
JVM在加載類的時候,都是通過ClassLoader的loadClass()方法來加載class的,loadClass(String name)方法:
使用的是雙親委托模式:
jvm啟動時,會啟動jre/rt.jar里的類加載器:bootstrap classloader,用來加載java核心api;然后啟動擴展類加載器ExtClassLoader加載擴展類,并加載用戶程序加載器AppClassLoader,并指定ExtClassLoader為他的父類;
當類被加載時,會先檢查在內(nèi)存中是否已經(jīng)被加載,如果是,則不再加載,如果沒有,再由AppClassLoader來加載,先從jar包里找,沒有再從classpath里找;
如果自定義loader類,就會存在這命名空間的情況,不同的加載器加載同一個類時,產(chǎn)生的實例其實是不同的;
Java代碼
public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); } public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); }
loadClass(String name)方法再調(diào)用loadClass(String name, boolean resolve)方法:
◆ name - 類的二進制名稱
◆ resolve - 如果該參數(shù)為 true,則分析這個類
Java代碼
protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // First, check if the class has already been loaded //JVM 規(guī)范規(guī)定ClassLoader可以在緩存保留它所加載的Class,如果一個Class已經(jīng)被加載過,則直接從緩存中獲取 Class c = findLoadedClass(name); if (c == null) { try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { // If still not found, then invoke findClass in order // to find the class. c = findClass(name); } } if (resolve) { resolveClass(c); } return c; } protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // First, check if the class has already been loaded //JVM 規(guī)范規(guī)定ClassLoader可以在緩存保留它所加載的Class,如果一個Class已經(jīng)被加載過,則直接從緩存中獲取 Class c = findLoadedClass(name); if (c == null) { try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { // If still not found, then invoke findClass in order // to find the class. c = findClass(name); } } if (resolve) { resolveClass(c); } return c; }
如果ClassLoader并沒有加載這個class,則調(diào)用findBootstrapClass0:
Java代碼
private Class findBootstrapClass0(String name) throws ClassNotFoundException { check(); if (!checkName(name)) throw new ClassNotFoundException(name); return findBootstrapClass(name); } private Class findBootstrapClass0(String name) throws ClassNotFoundException { check(); if (!checkName(name)) throw new ClassNotFoundException(name); return findBootstrapClass(name); }
該方法會調(diào)用check()方法來判斷這個類是否已經(jīng)初始化,并且通過checkName(name)來判斷由name指定的這個類是否存在***調(diào)用findBootstrapClass(name):
Java代碼
private native Class findBootstrapClass(String name) throws ClassNotFoundException; private native Class findBootstrapClass(String name) throws ClassNotFoundException;
而這個findBootstrapClass方法是一個native方法,這是我們的root loader,這個載入方法并非是由JAVA所寫,而是C++寫的,它會最終調(diào)用JVM中的原生findBootstrapClass方法來完成類的加載。
如果上面兩個都找不到,則使用findClass(name)來查找指定類名的Class:
Java代碼
protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); } protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); }
JDK5.0中的說明:
使用指定的二進制名稱查找類。此方法應該被類加載器的實現(xiàn)重寫,該實現(xiàn)按照委托模型來加載類。在通過父類加載器檢查所請求的類后,此方法將被 loadClass 方法調(diào)用。默認實現(xiàn)拋出一個ClassNotFoundException。
所以,我們在自定義類中,只需要重寫findClass()即可。
MyClassLoader類:
Java代碼
public class MyClassLoader extends ClassLoader { private String fileName; public MyClassLoader(String fileName) { this.fileName = fileName; } protected Class<?> findClass(String className) throws ClassNotFoundException { Class clazz = this.findLoadedClass(className); if (null == clazz) { try { String classFile = getClassFile(className); FileInputStream fis = new FileInputStream(classFile); FileChannel fileC = fis.getChannel(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel outC = Channels.newChannel(baos); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { int i = fileC.read(buffer); if (i == 0 || i == -1) { break; } buffer.flip(); outC.write(buffer); buffer.clear(); } fis.close(); byte[] bytes = baos.toByteArray(); clazz = defineClass(className, bytes, 0, bytes.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return clazz; } private byte[] loadClassBytes(String className) throws ClassNotFoundException { try { String classFile = getClassFile(className); FileInputStream fis = new FileInputStream(classFile); FileChannel fileC = fis.getChannel(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel outC = Channels.newChannel(baos); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { int i = fileC.read(buffer); if (i == 0 || i == -1) { break; } buffer.flip(); outC.write(buffer); buffer.clear(); } fis.close(); return baos.toByteArray(); } catch (IOException fnfe) { throw new ClassNotFoundException(className); } } private String getClassFile(String name) { StringBuffer sb = new StringBuffer(fileName); name = name.replace('.', File.separatorChar) + ".class"; sb.append(File.separator + name); return sb.toString(); } } public class MyClassLoader extends ClassLoader { private String fileName; public MyClassLoader(String fileName) { this.fileName = fileName; } protected Class<?> findClass(String className) throws ClassNotFoundException { Class clazz = this.findLoadedClass(className); if (null == clazz) { try { String classFile = getClassFile(className); FileInputStream fis = new FileInputStream(classFile); FileChannel fileC = fis.getChannel(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel outC = Channels.newChannel(baos); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { int i = fileC.read(buffer); if (i == 0 || i == -1) { break; } buffer.flip(); outC.write(buffer); buffer.clear(); } fis.close(); byte[] bytes = baos.toByteArray(); clazz = defineClass(className, bytes, 0, bytes.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return clazz; } private byte[] loadClassBytes(String className) throws ClassNotFoundException { try { String classFile = getClassFile(className); FileInputStream fis = new FileInputStream(classFile); FileChannel fileC = fis.getChannel(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel outC = Channels.newChannel(baos); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { int i = fileC.read(buffer); if (i == 0 || i == -1) { break; } buffer.flip(); outC.write(buffer); buffer.clear(); } fis.close(); return baos.toByteArray(); } catch (IOException fnfe) { throw new ClassNotFoundException(className); } } private String getClassFile(String name) { StringBuffer sb = new StringBuffer(fileName); name = name.replace('.', File.separatorChar) + ".class"; sb.append(File.separator + name); return sb.toString(); } }
該類中通過調(diào)用defineClass(String name, byte[] b, int off, int len)方法來定義一個類:
Java代碼
protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError { return defineClass(name, b, off, len, null); } protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError { return defineClass(name, b, off, len, null); }
注:MyClassLoader加載類時有一個局限,必需指定.class文件,而不能指定.jar文件。該類中的大部分代碼是從網(wǎng)上搜索到的,是出自一牛人之筆,只是不知道原帖在哪,希望不會被隱藏。
MainClassLoader類:
Java代碼
public class MainClassLoader { public static void main(String[] args) { try { MyClassLoader tc = new MyClassLoader("F:\\OpenLib\\"); Class c = tc.findClass("Test"); c.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } } public class MainClassLoader { public static void main(String[] args) { try { MyClassLoader tc = new MyClassLoader("F:\\OpenLib\\"); Class c = tc.findClass("Test"); c.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } }
***是一個簡單的Test測試類:
Java代碼
public class Test { public Test() { System.out.println("Test"); } public static void main(String[] args) { System.out.println("Hello World"); } }
“Java的ClassLoader機制是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。