p { margin-bottom: 0.21cm; }
自定义类加载器的类需要继承ClassLoader 类 重写findClass 方法 重写findClass 方法后 才能够正确的优先父类寻找
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// 得到文件的路径名
String fileName = ClassDir + "//" + name.substring(name.lastIndexOf( '.' )+1) + ".class" ;
try {
// 加载这个文件
FileInputStream fis = new FileInputStream(fileName);
// 定义字节数组流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 给文件解密 解密到 bos 里面
cypher (fis, bos);
fis.close();
// 得到字节数组
byte [] bytes = bos.toByteArray();
// 把一个字节数组生成 class
return defineClass (bytes, 0, bytes. length );
} catch (Exception e) {
e.printStackTrace();
}
return super .findClass(name);
}