{"id":415,"date":"2023-09-16T11:02:37","date_gmt":"2023-09-16T03:02:37","guid":{"rendered":""},"modified":"2023-09-16T11:02:37","modified_gmt":"2023-09-16T03:02:37","slug":"java \u52a8\u6001\u52a0\u8f7d class_Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e00)\u4e4bJava\u4e2d\u7684ClassLoader","status":"publish","type":"post","link":"https:\/\/mushiming.com\/415.html","title":{"rendered":"java \u52a8\u6001\u52a0\u8f7d class_Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e00)\u4e4bJava\u4e2d\u7684ClassLoader"},"content":{"rendered":"
\u76ee\u5f55<\/p>\n
\u6211\u4eec\u7684\u8ba4\u8bc6<\/p>\n
\u6211\u4eec\u90fd\u77e5\u9053, \u521a\u5199\u597d\u7684Java\u7684\u6e90\u6587\u4ef6\u662f\u4ee5.java\u4e3a\u6269\u5c55\u540d\u7684, \u9700\u8981\u8ba9JVM\u53bb\u89e3\u6790\u7684\u65f6\u5019, \u5fc5\u987b\u7f16\u8bd1\u6210Java\u5b57\u8282\u7801, \u800cJava\u5b57\u8282\u7801\u6587\u4ef6\u5c31\u662f\u4ee5.class\u4e3a\u6269\u5c55\u540d\u7684. ClassLoader\u7684\u5bf9\u8c61\u5c31\u662f\u53bb\u52a0\u8f7d\u8fd9\u4e9bJava\u5b57\u8282\u7801\u6587\u4ef6<\/p>\n
Java\u9ed8\u8ba4\u7684ClassLoader<\/p>\n
\u5728Java\u9ed8\u8ba4\u73af\u5883\u4e2d, \u63d0\u4f9b\u4e86\u4e09\u79cdClassloader<\/p>\n
BootStrap ClassLoader : \u542f\u52a8\u7c7b\u52a0\u8f7d\u5668\uff0cJava\u7c7b\u52a0\u8f7d\u5668\u4e2d\u6700\u9876\u5c42\u7684\u7c7b\u52a0\u8f7d\u5668\uff0c\u8d1f\u8d23\u52a0\u8f7dJDK\u4e2d\u7684\u6838\u5fc3\u7c7b\u5e93\uff0c\u5982\uff1art.jar\u3001resources.jar\u3001charsets.jar\u7b49<\/p>\n
Extension ClassLoader : \u6269\u5c55\u7c7b\u52a0\u8f7d\u5668\uff0c\u8d1f\u8d23\u52a0\u8f7dJava\u7684\u6269\u5c55\u7c7b\u5e93\uff0c\u9ed8\u8ba4\u52a0\u8f7dJAVA_HOME\/jre\/lib\/ext\/\u76ee\u4e0b\u7684\u6240\u6709jar<\/p>\n
App ClassLoader : \u7cfb\u7edf\u7c7b\u52a0\u8f7d\u5668\uff0c\u8d1f\u8d23\u52a0\u8f7d\u5e94\u7528\u7a0b\u5e8fclasspath\u76ee\u5f55\u4e0b\u7684\u6240\u6709jar\u548cclass\u6587\u4ef6<\/p>\n
<\/p>\n
java_classloader.jpg<\/p>\n
\u6709\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u901a\u8fc7Class.getClassLoader()\u53bb\u9a8c\u8bc1\u4e00\u4e0b.<\/p>\n
ClassLoader\u52a0\u8f7d\u539f\u7406<\/p>\n
\u52a0\u8f7d\u987a\u5e8f(\u53cc\u4eb2\u59d4\u6258\u673a\u5236)<\/p>\n
\u5982\u679c\u6211\u4eec\u81ea\u5b9a\u4e49\u4e00\u4e2aClassLoader\u53eb\u505aMyClassLoader, \u5f53\u67e5\u627e\u9700\u8981\u7684\u7c7b\u7684\u65f6\u5019, MyClassLoader\u4f1a\u628a\u8be5\u8d23\u4efb\u59d4\u6258\u7ed9\u5b83\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\u4e0d\u662f\u901a\u8fc7\u7ee7\u627f\u7684\u7236\u7c7b, \u662f\u901a\u8fc7parent\u5c5e\u6027\u53bb\u6301\u6709. \u6574\u4e2a\u8fc7\u7a0b\u4ece\u9876\u5230\u4e0b, \u5982\u679c\u4e00\u4e2a\u7c7b\u627e\u4e0d\u5230, \u90a3\u4e48\u67e5\u627e\u987a\u5e8f\u662fBootStrap ClassLoader->Extension ClassLoader->App ClassLoader->MyClassLoader->\u629b\u51fa\u5f02\u5e38ClassNotFoundException<\/p>\n
\u4e24\u4e2a\u7c7b\u662f\u5426\u76f8\u7b49<\/p>\n
\u4e00\u822c\u6211\u4eec\u8ba4\u4e3a\u4e24\u4e2a\u7c7b\u662f\u5426\u76f8\u7b49, \u4ec5\u4ec5\u901a\u8fc7\u5305\u540d+\u7c7b\u540d\u53bb\u533a\u5206. \u4e8b\u5b9e\u4e0a\u8fd8\u6709\u4e00\u4e2a\u6761\u4ef6, \u5c31\u662f\u52a0\u8f7d\u8be5\u7c7b\u7684ClassLoader\u662f\u5426\u662f\u540c\u4e00\u4e2a<\/p>\n
\u4e24\u4e2a\u5173\u952e\u65b9\u6cd5<\/p>\n
ClassLoader\u4e2d\u8ba4\u4e3a\u6bd4\u8f83\u5173\u952e\u7684\u65b9\u6cd5\u6709\u4e09\u4e2a:<\/p>\n
public Class> loadClass(String name) : \u6839\u636e\u53cc\u4eb2\u59d4\u6258\u673a\u5236\u7684\u65b9\u5f0f\u67e5\u627eclass<\/p>\n
protected Class> findClass(String name) : \u6839\u636e\u8be5ClassLoader\u7684\u65b9\u5f0f\u53bb\u67e5\u627eclass<\/p>\n
protected final Class> defineClass() : \u8be5\u65b9\u6cd5\u6709\u591a\u4e2a\u91cd\u8f7d, \u5177\u4f53\u53c2\u6570\u4e0d\u5199\u4e86. \u4e3b\u8981\u4f5c\u7528\u662f\u628a\u7c7b\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d<\/p>\n
loadClass\u4e3a\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5, \u6709\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u53bb\u770b\u770b\u6e90\u7801. \u6240\u4ee5\u4e00\u822c\u6211\u4eec\u53ea\u9700\u8981\u91cd\u5199findClass\u5373\u53ef.<\/p>\n
\u4e0a\u9762\u4e09\u4e2a\u65b9\u6cd5\u6709\u5173\u7cfb\u7684, loadClass->findClass->defineClass. loadClass\u4f7f\u7528\u53cc\u4eb2\u59d4\u6258\u673a\u5236\u53bb\u67e5\u627e\u8981\u52a0\u8f7d\u7684\u7c7b, \u5f53\u4e0a\u5c42ClassLoader\u4e0d\u80fd\u52a0\u8f7d\u8be5\u7c7b\u65f6, \u5c31\u4f1a\u53bb\u4f7f\u7528\u81ea\u5df1\u7684findClass\u53bb\u52a0\u8f7d\u7c7b. \u52a0\u8f7d\u5230\u7684\u7c7b(\u53ef\u80fd\u4e3a\u7a7a)\u4f1a\u4f20\u5165defineClass\u4e2d, \u5982\u679c\u4e3a\u7a7a\u5219\u629b\u51fa\u5f02\u5e38.<\/p>\n
\u81ea\u5b9a\u4e49ClassLoader<\/p>\n
\u6211\u4eec\u4e0b\u9762\u81ea\u5b9a\u4e49\u4e00\u4e2aMyClassLoader\u53bb\u52a0\u8f7d\u5916\u90e8\u7684RemoteClass.class\u6587\u4ef6<\/p>\n
RemoteClass.class<\/p>\n
package top.august1996.demo;<\/p>\n
public class RemoteClass {
\n <\/p>\n
public void catched() {
\n <\/p>\n
System.out.println(\"I am catched...\");<\/p>\n
}<\/p>\n
}<\/p>\n
\u6211\u4eec\u628a\u4e0a\u9762\u6587\u4ef6\u79fb\u5230\u4efb\u610f\u76ee\u5f55(\/Users\/August\/Desktop)<\/p>\n
cd \/Users\/August\/Desktop<\/p>\n
javac RemoteClass.java<\/p>\n
\u73b0\u5728\u6211\u4eec\u5c31\u5728\u684c\u9762\u4e0a\u7f16\u8bd1\u4e86\u4e00\u4e2a\u5b57\u8282\u7801\u6587\u4ef6<\/p>\n
MyClassLoader.java<\/p>\n
package top.august1996.demo;<\/p>\n
import java.io.ByteArrayOutputStream;<\/p>\n
import java.io.File;<\/p>\n
import java.io.FileInputStream;<\/p>\n
import java.io.IOException;<\/p>\n
public class MyClassLoader extends ClassLoader {
\n <\/p>\n
private String mClassPath; \/\/ Class\u5b58\u653e\u7684\u7684\u76ee\u5f55<\/p>\n
public MyClassLoader(String classPath) {
\n <\/p>\n
mClassPath = classPath;<\/p>\n
}<\/p>\n
@Override<\/p>\n
protected Class> findClass(String name) {
\n <\/p>\n
File clsFile = new File(mClassPath, getClassName(name));<\/p>\n
FileInputStream fis = null;<\/p>\n
ByteArrayOutputStream baos = new ByteArrayOutputStream();<\/p>\n
byte[] b = null;<\/p>\n
try {
\n <\/p>\n
fis = new FileInputStream(clsFile);<\/p>\n
int data = 0;<\/p>\n
while ((data = fis.read()) != -1) {
\n <\/p>\n
baos.write(data);<\/p>\n
}<\/p>\n
b = baos.toByteArray();<\/p>\n
} catch (Exception e) {
\n <\/p>\n
e.printStackTrace();<\/p>\n
} finally {
\n <\/p>\n
if (fis != null) {
\n <\/p>\n
try {
\n <\/p>\n
fis.close();<\/p>\n
} catch (IOException e) {
\n <\/p>\n
e.printStackTrace();<\/p>\n
}<\/p>\n
}<\/p>\n
if (baos != null) {
\n <\/p>\n
try {
\n <\/p>\n
baos.close();<\/p>\n
} catch (IOException e) {
\n <\/p>\n
e.printStackTrace();<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n
\/\/ \u4e0a\u9762\u662f\u57fa\u672c\u7684\u6570\u636e\u6d41\u64cd\u4f5c, \u628aClass\u6587\u4ef6\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u6d41<\/p>\n
return defineClass(name, b, 0, b.length);<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u83b7\u53d6Class\u7684\u5b8c\u6574\u6587\u4ef6\u540d<\/p>\n
*<\/p>\n
* @param name<\/p>\n
* @return<\/p>\n
*\/<\/p>\n
private String getClassName(String name) {
\n <\/p>\n
String clsName = null;<\/p>\n
if (name != null) {
\n <\/p>\n
int lastIndexOf = name.lastIndexOf(\".\");<\/p>\n
if (lastIndexOf == -1) {
\n <\/p>\n
clsName = name + \".class\";<\/p>\n
} else {
\n <\/p>\n
clsName = name.substring(lastIndexOf + 1) + \".class\";<\/p>\n
}<\/p>\n
}<\/p>\n
return clsName;<\/p>\n
}<\/p>\n
}<\/p>\n
TestDemo.java<\/p>\n
package top.august1996.demo;<\/p>\n
import java.lang.reflect.Method;<\/p>\n
public class TestDemo {
\n <\/p>\n
public static void main(String[] args) throws Exception {
\n <\/p>\n
MyClassLoader classLoader = new MyClassLoader(\"\/Users\/August\/Desktop\");<\/p>\n
Class> cls = classLoader.findClass(\"top.august1996.demo.RemoteClass\");<\/p>\n
Object object = cls.newInstance();<\/p>\n
Method method = cls.getDeclaredMethod(\"catched\", null);<\/p>\n
method.invoke(object, null);<\/p>\n
}<\/p>\n
}<\/p>\n
I am catched...<\/p>\n
\u6211\u4eec\u4f7f\u7528\u53cd\u5c04\u533a\u8c03\u7528\u81ea\u5df1\u52a0\u8f7d\u7684RemoteClass\u7684catched\u65b9\u6cd5, \u53ef\u4ee5\u770b\u5230\u7ed3\u679c\u5df2\u7ecf\u662f\u6210\u529f\u7684\u4e86.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"java \u52a8\u6001\u52a0\u8f7d class_Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e00)\u4e4bJava\u4e2d\u7684ClassLoader\u76ee\u5f55\u6211\u4eec\u7684\u8ba4\u8bc6\u6211\u4eec\u90fd\u77e5\u9053,\u521a\u5199\u597d\u7684Java\u7684\u6e90\u6587\u4ef6\u662f\u4ee5.j...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/415"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}