??????Java???????
???????????? ???????[ 2015/7/7 15:34:06 ] ??????????????????? ???????
????4.????????????
public class MyClassLoader extends ClassLoader{
private String rootPath;
public MyClassLoader(String rootPath){
this.rootPath = rootPath;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
//check if the class have been loaded
Class<?> c = findLoadedClass(name);
if(c!=null){
return c;
}
//load the class
byte[] classData = getClassData(name);
if(classData==null){
throw new ClassNotFoundException();
}
else{
c = defineClass(name??classData?? 0?? classData.length);
return c;
}
}
private byte[] getClassData(String className){
String path = rootPath+"/"+className.replace('.'?? '/')+".class";
InputStream is = null;
ByteArrayOutputStream bos = null;
try {
is = new FileInputStream(path);
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int temp = 0;
while((temp = is.read(buffer))!=-1){
bos.write(buffer??0??temp);
}
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
is.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
?????????????????????
?????????????????HelloWorld
????package testOthers;
????public class HelloWorld {
????}
??????D????????????testOthers????У?????HelloWorld.java?????????class??????testOthers????????
???????????′?????в???
????public class testMyClassLoader {
????@Test
????public void test() throws Exception{
????MyClassLoader loader = new MyClassLoader("D:");
????Class<?> c = loader.loadClass("testOthers.HelloWorld");
????System.out.println(c.getClassLoader());
????}
????}
?????????
???????HelloWorld??????????????????????MyClassLoader?????
????5.???????????
????JVM???????????????????裺????Load?????????Link????????(Initialize)
????1) ????
??????????????????????????
????2)?????
???????????????????????????JVM?淶????а???????????
?????????????????????????棬?????????????????
????????????????????????е??????????????????á?
????3)???????
????????????????????????????
????ps:????????????????£?Java ?У??????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????????????????JVM????????Щ???????int a = 5;?е?a?????????????????????????????????????е???????????????
??????????????裺
????1????????????б?????????????????м????????
????2???????????????????????????????б????????????????????????У????????????Σ???????????????????????????
????3????????д???static?????飬???????????Щ????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11