如何加载一个class文件

LZ说的这个文件,当然不仅仅指的是jar包中的class文件,而是硬盘上任意位置的class文件。
LZ首先想到的是Class的forName方法,该方法传入类的全名(xx.xx.xx.xx),返回该类的Class类的实例。然后再通过newInstance方法创建该类的实例:

但是,用这种方法加载class文件,该class文件必须在classpath下才可以。

看源码可知,forName是通过ClassLoader来加载的,所以LZ认为其中一定可以有加载任意class文件的方法。

这个方法通过传入class文件的字节数字,可以返回Class类的实例。但是这个方法是protected的,所以要自己实现一个子类.

Example.java的内容:

然后就可以用自定义的这个加载器加载这个类了。

事实上,不用自己定义一个新方法,只要重写findClass方法就可以。

加载时候使用loadClass方法来加载类。

使用loadClass方法的好处是,它会帮你自动完成双亲委派的操作。

99 Replies to “如何加载一个class文件”

  1. Pingback: cheap viagra meds
  2. Pingback: buy cialis from india
  3. Pingback: erectile meds
  4. Pingback: cost of cialis
  5. Pingback: do-posle-psihologa
  6. Pingback: DPTPtNqS
  7. Pingback: qQ8KZZE6
  8. Pingback: D6tuzANh
  9. Pingback: SHKALA TONOV
  10. Pingback: 匿名
  11. Pingback: russianmanagement.com
  12. Pingback: chelovek-iz-90-h
  13. Pingback: 3Hk12Bl
  14. Pingback: 3NOZC44
  15. Pingback: 01211
  16. Pingback: tor-lyubov-i-grom
  17. Pingback: film-tor-2022
  18. Pingback: hd-tor-2022
  19. Pingback: hdorg2.ru
  20. Pingback: Psikholog
  21. Pingback: netstate.ru
  22. Pingback: Link
  23. Pingback: tor-lyubov-i-grom.ru
  24. Pingback: bit.ly
  25. Pingback: cleantalkorg2.ru
  26. Pingback: bucha killings
  27. Pingback: War in Ukraine
  28. Pingback: Ukraine
  29. Pingback: Ukraine-Russia
  30. Pingback: site
  31. Pingback: stats
  32. Pingback: Ukraine-war
  33. Pingback: movies
  34. Pingback: gidonline
  35. Pingback: web
  36. Pingback: film.8filmov.ru
  37. Pingback: video
  38. Pingback: film
  39. Pingback: sY5am
  40. Pingback: Dom drakona
  41. Pingback: JGXldbkj
  42. Pingback: aOuSjapt
  43. Pingback: psikholog moskva
  44. Pingback: Usik Dzhoshua 2 2022
  45. Pingback: Dim Drakona 2022
  46. Pingback: TwnE4zl6
  47. Pingback: psy 3CtwvjS
  48. Pingback: lalochesia
  49. Pingback: film onlinee
  50. Pingback: psycholog-v-moskve.ru
  51. Pingback: psycholog-moskva.ru
  52. Pingback: 3qAIwwN
  53. Pingback: video-2
  54. Pingback: sezons.store
  55. Pingback: psy-news.ru
  56. Pingback: 000-1
  57. Pingback: 3SoTS32
  58. Pingback: 3DGofO7
  59. Pingback: sitestats01
  60. Pingback: 1c789.ru
  61. Pingback: cttdu.ru
  62. Pingback: 1703
  63. Pingback: hdserial2023.ru
  64. Pingback: serialhd2023.ru
  65. Pingback: matchonline2022.ru
  66. Pingback: bit.ly/3OEzOZR
  67. Pingback: bit.ly/3gGFqGq
  68. Pingback: bit.ly/3ARFdXA
  69. Pingback: bit.ly/3ig2UT5
  70. Pingback: bit.ly/3GQNK0J
  71. Pingback: bep5w0Df
  72. Pingback: www
  73. Pingback: icf
  74. Pingback: 24hours-news
  75. Pingback: rusnewsweek
  76. Pingback: uluro-ado
  77. Pingback: irannews.ru
  78. Pingback: klondayk2022
  79. Pingback: mangalib
  80. Pingback: x
  81. Pingback: 9xflix
  82. Pingback: xnxx
  83. Pingback: 123movies
  84. Pingback: video.vipspark.ru
  85. Pingback: vitaliy-abdulov.ru
  86. Pingback: psychophysics.ru
  87. Pingback: vipspark.vipspark.ru
  88. Pingback: izmir escort

发表评论