如题,作者之前对于自己写的java.lang.String类能否被加载,一直感到非常困惑,今天写代码证实了一下。上干货。
自己写类加载器:
public class MyClassLoader extends ClassLoader{
public Class findClass(String name){
byte[] b = null;
try {
b = getByte(name);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return defineClass(null, b, 0, b.length);
}
private byte[] getByte(String name) throws IOException{
FileInputStream in = new FileInputStream(name);
byte[] b = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len = 0;
while((len = in.read(b)) != -1){
out.write(b, 0, len);
}
out.close();
b = out.toByteArray();
return b;
}
}
测试类(path1,path2是我存放编译后的class文件的路径):
public class MyClassLoaderTest {
public static void main(String [] args) throws Exception{
MyClassLoader myloader = new MyClassLoader