JVM在加載類的時候,都是通過ClassLoader的loadClass()方法來加載class的,loadClass(String name)方法:
使用的是雙親委托模式:
jvm啟動時,會啟動jre/rt.jar裡的類加載器:bootstrap classloader,用來加載Java核心api;然後啟動擴展類加載器ExtClassLoader加載擴展類,並加載用戶程序加載器AppClassLoader,並指定ExtClassLoader為他的父類;
當類被加載時,會先檢查在內存中是否已經被加載,如果是,則不再加載,如果沒有,再由AppClassLoader來加載,先從jar包裡找,沒有再從classpath裡找;
如果自定義loader類,就會存在這命名空間的情況,不同的加載器加載同一個類時,產生的實例其實是不同的;
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)方法再調用loadClass(String name, boolean resolve)方法:
◆ name - 類的二進制名稱
◆ resolve - 如果該參數為 true,則分析這個類
Java代碼
- protected synchronized Class<?> loadClass(String name, boolean resolve)
- throws ClassNotFoundException
- {
- // First, check if the class has already been loaded
- //JVM 規范規定ClassLoader可以在緩存保留它所加載的Class,如果一個Class已經被加載過,則直接從緩存中獲取
- 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 規范規定ClassLoader可以在緩存保留它所加載的Class,如果一個Class已經被加載過,則直接從緩存中獲取
- 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,則調用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);
- }
該方法會調用check()方法來判斷這個類是否已經初始化,並且通過checkName(name)來判斷由name指定的這個類是否存在最後調用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++寫的,它會最終調用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中的說明:
使用指定的二進制名稱查找類。此方法應該被類加載器的實現重寫,該實現按照委托模型來加載類。在通過父類加載器檢查所請求的類後,此方法將被 loadClass 方法調用。默認實現拋出一個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();
- }
- }
該類中通過調用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文件。該類中的大部分代碼是從網上搜索到的,是出自一牛人之筆,只是不知道原帖在哪,希望不會被隱藏。
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");
- }
- }