程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java的ClassLoader機制解析

Java的ClassLoader機制解析

編輯:關於JAVA

JVM在加載類的時候,都是通過ClassLoader的loadClass()方法來加載class的,loadClass(String name)方法:

使用的是雙親委托模式:

jvm啟動時,會啟動jre/rt.jar裡的類加載器:bootstrap classloader,用來加載Java核心api;然後啟動擴展類加載器ExtClassLoader加載擴展類,並加載用戶程序加載器AppClassLoader,並指定ExtClassLoader為他的父類;

當類被加載時,會先檢查在內存中是否已經被加載,如果是,則不再加載,如果沒有,再由AppClassLoader來加載,先從jar包裡找,沒有再從classpath裡找;

如果自定義loader類,就會存在這命名空間的情況,不同的加載器加載同一個類時,產生的實例其實是不同的;

Java代碼

  1. public Class<?> loadClass(String name) throws ClassNotFoundException {
  2. return loadClass(name, false);
  3. }
  4. public Class<?> loadClass(String name) throws ClassNotFoundException {
  5. return loadClass(name, false);
  6. }

loadClass(String name)方法再調用loadClass(String name, boolean resolve)方法:

◆ name - 類的二進制名稱

◆ resolve - 如果該參數為 true,則分析這個類

Java代碼

  1. protected synchronized Class<?> loadClass(String name, boolean resolve)
  2. throws ClassNotFoundException
  3. {
  4. // First, check if the class has already been loaded
  5. //JVM 規范規定ClassLoader可以在緩存保留它所加載的Class,如果一個Class已經被加載過,則直接從緩存中獲取
  6. Class c = findLoadedClass(name);
  7. if (c == null) {
  8. try {
  9. if (parent != null) {
  10. c = parent.loadClass(name, false);
  11. } else {
  12. c = findBootstrapClass0(name);
  13. }
  14. } catch (ClassNotFoundException e) {
  15. // If still not found, then invoke findClass in order
  16. // to find the class.
  17. c = findClass(name);
  18. }
  19. }
  20. if (resolve) {
  21. resolveClass(c);
  22. }
  23. return c;
  24. }
  25. protected synchronized Class<?> loadClass(String name, boolean resolve)
  26. throws ClassNotFoundException
  27. {
  28. // First, check if the class has already been loaded
  29. //JVM 規范規定ClassLoader可以在緩存保留它所加載的Class,如果一個Class已經被加載過,則直接從緩存中獲取
  30. Class c = findLoadedClass(name);
  31. if (c == null) {
  32. try {
  33. if (parent != null) {
  34. c = parent.loadClass(name, false);
  35. } else {
  36. c = findBootstrapClass0(name);
  37. }
  38. } catch (ClassNotFoundException e) {
  39. // If still not found, then invoke findClass in order
  40. // to find the class.
  41. c = findClass(name);
  42. }
  43. }
  44. if (resolve) {
  45. resolveClass(c);
  46. }
  47. return c;
  48. }

如果ClassLoader並沒有加載這個class,則調用findBootstrapClass0:

Java代碼

  1. private Class findBootstrapClass0(String name)
  2. throws ClassNotFoundException
  3. {
  4. check();
  5. if (!checkName(name))
  6. throw new ClassNotFoundException(name);
  7. return findBootstrapClass(name);
  8. }
  9. private Class findBootstrapClass0(String name)
  10. throws ClassNotFoundException
  11. {
  12. check();
  13. if (!checkName(name))
  14. throw new ClassNotFoundException(name);
  15. return findBootstrapClass(name);
  16. }

該方法會調用check()方法來判斷這個類是否已經初始化,並且通過checkName(name)來判斷由name指定的這個類是否存在最後調用findBootstrapClass(name):

Java代碼

  1. private native Class findBootstrapClass(String name)
  2. throws ClassNotFoundException;
  3. private native Class findBootstrapClass(String name)
  4. throws ClassNotFoundException;

而這個findBootstrapClass方法是一個native方法,這是我們的root loader,這個載入方法並非是由Java所寫,而是C++寫的,它會最終調用JVM中的原生findBootstrapClass方法來完成類的加載。

如果上面兩個都找不到,則使用findClass(name)來查找指定類名的Class:

Java代碼

  1. protected Class<?> findClass(String name) throws ClassNotFoundException {
  2. throw new ClassNotFoundException(name);
  3. }
  4. protected Class<?> findClass(String name) throws ClassNotFoundException {
  5. throw new ClassNotFoundException(name);
  6. }

JDK5.0中的說明:

使用指定的二進制名稱查找類。此方法應該被類加載器的實現重寫,該實現按照委托模型來加載類。在通過父類加載器檢查所請求的類後,此方法將被 loadClass 方法調用。默認實現拋出一個ClassNotFoundException。

所以,我們在自定義類中,只需要重寫findClass()即可。

MyClassLoader類:

Java代碼

  1. public class MyClassLoader extends ClassLoader {
  2. private String fileName;
  3. public MyClassLoader(String fileName) {
  4. this.fileName = fileName;
  5. }
  6. protected Class<?> findClass(String className) throws ClassNotFoundException {
  7. Class clazz = this.findLoadedClass(className);
  8. if (null == clazz) {
  9. try {
  10. String classFile = getClassFile(className);
  11. FileInputStream fis = new FileInputStream(classFile);
  12. FileChannel fileC = fis.getChannel();
  13. ByteArrayOutputStream baos = new
  14. ByteArrayOutputStream();
  15. WritableByteChannel outC = Channels.newChannel(baos);
  16. ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
  17. while (true) {
  18. int i = fileC.read(buffer);
  19. if (i == 0 || i == -1) {
  20. break;
  21. }
  22. buffer.flip();
  23. outC.write(buffer);
  24. buffer.clear();
  25. }
  26. fis.close();
  27. byte[] bytes = baos.toByteArray();
  28. clazz = defineClass(className, bytes, 0, bytes.length);
  29. } catch (FileNotFoundException e) {
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. return clazz;
  36. }
  37. private byte[] loadClassBytes(String className) throws
  38. ClassNotFoundException {
  39. try {
  40. String classFile = getClassFile(className);
  41. FileInputStream fis = new FileInputStream(classFile);
  42. FileChannel fileC = fis.getChannel();
  43. ByteArrayOutputStream baos = new
  44. ByteArrayOutputStream();
  45. WritableByteChannel outC = Channels.newChannel(baos);
  46. ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
  47. while (true) {
  48. int i = fileC.read(buffer);
  49. if (i == 0 || i == -1) {
  50. break;
  51. }
  52. buffer.flip();
  53. outC.write(buffer);
  54. buffer.clear();
  55. }
  56. fis.close();
  57. return baos.toByteArray();
  58. } catch (IOException fnfe) {
  59. throw new ClassNotFoundException(className);
  60. }
  61. }
  62. private String getClassFile(String name) {
  63. StringBuffer sb = new StringBuffer(fileName);
  64. name = name.replace('.', File.separatorChar) + ".class";
  65. sb.append(File.separator + name);
  66. return sb.toString();
  67. }
  68. }
  69. public class MyClassLoader extends ClassLoader {
  70. private String fileName;
  71. public MyClassLoader(String fileName) {
  72. this.fileName = fileName;
  73. }
  74. protected Class<?> findClass(String className) throws ClassNotFoundException {
  75. Class clazz = this.findLoadedClass(className);
  76. if (null == clazz) {
  77. try {
  78. String classFile = getClassFile(className);
  79. FileInputStream fis = new FileInputStream(classFile);
  80. FileChannel fileC = fis.getChannel();
  81. ByteArrayOutputStream baos = new
  82. ByteArrayOutputStream();
  83. WritableByteChannel outC = Channels.newChannel(baos);
  84. ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
  85. while (true) {
  86. int i = fileC.read(buffer);
  87. if (i == 0 || i == -1) {
  88. break;
  89. }
  90. buffer.flip();
  91. outC.write(buffer);
  92. buffer.clear();
  93. }
  94. fis.close();
  95. byte[] bytes = baos.toByteArray();
  96. clazz = defineClass(className, bytes, 0, bytes.length);
  97. } catch (FileNotFoundException e) {
  98. e.printStackTrace();
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. return clazz;
  104. }
  105. private byte[] loadClassBytes(String className) throws
  106. ClassNotFoundException {
  107. try {
  108. String classFile = getClassFile(className);
  109. FileInputStream fis = new FileInputStream(classFile);
  110. FileChannel fileC = fis.getChannel();
  111. ByteArrayOutputStream baos = new
  112. ByteArrayOutputStream();
  113. WritableByteChannel outC = Channels.newChannel(baos);
  114. ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
  115. while (true) {
  116. int i = fileC.read(buffer);
  117. if (i == 0 || i == -1) {
  118. break;
  119. }
  120. buffer.flip();
  121. outC.write(buffer);
  122. buffer.clear();
  123. }
  124. fis.close();
  125. return baos.toByteArray();
  126. } catch (IOException fnfe) {
  127. throw new ClassNotFoundException(className);
  128. }
  129. }
  130. private String getClassFile(String name) {
  131. StringBuffer sb = new StringBuffer(fileName);
  132. name = name.replace('.', File.separatorChar) + ".class";
  133. sb.append(File.separator + name);
  134. return sb.toString();
  135. }
  136. }

該類中通過調用defineClass(String name, byte[] b, int off, int len)方法來定義一個類:

Java代碼

  1. protected final Class<?> defineClass(String name, byte[] b, int off, int len)
  2. throws ClassFormatError
  3. {
  4. return defineClass(name, b, off, len, null);
  5. }
  6. protected final Class<?> defineClass(String name, byte[] b, int off, int len)
  7. throws ClassFormatError
  8. {
  9. return defineClass(name, b, off, len, null);
  10. }

注:MyClassLoader加載類時有一個局限,必需指定.class文件,而不能指定.jar文件。該類中的大部分代碼是從網上搜索到的,是出自一牛人之筆,只是不知道原帖在哪,希望不會被隱藏。

MainClassLoader類:

Java代碼

  1. public class MainClassLoader {
  2. public static void main(String[] args) {
  3. try {
  4. MyClassLoader tc = new MyClassLoader("F:\\OpenLib\\");
  5. Class c = tc.findClass("Test");
  6. c.newInstance();
  7. } catch (ClassNotFoundException e) {
  8. e.printStackTrace();
  9. } catch (IllegalAccessException e) {
  10. e.printStackTrace();
  11. } catch (InstantiationException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. }
  16. public class MainClassLoader {
  17. public static void main(String[] args) {
  18. try {
  19. MyClassLoader tc = new MyClassLoader("F:\\OpenLib\\");
  20. Class c = tc.findClass("Test");
  21. c.newInstance();
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace();
  24. } catch (IllegalAccessException e) {
  25. e.printStackTrace();
  26. } catch (InstantiationException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }

最後是一個簡單的Test測試類:

Java代碼

  1. public class Test
  2. {
  3. public Test() {
  4. System.out.println("Test");
  5. }
  6. public static void main(String[] args) {
  7. System.out.println("Hello World");
  8. }
  9. }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved