package com.itm.one;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyClassLoader extends ClassLoader{
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String srcPath = args[0];
String destDir = args[1];
FileInputStream fis = new FileInputStream(srcPath);
String destFileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);
String destPath = destDir + "\\" + destFileName;
FileOutputStream fos = new FileOutputStream(destPath);
cypher(fis,fos);
fis.close();
fos.close();
}
private static void cypher(InputStream ips ,OutputStream ops) throws Exception{
int b = -1;
while((b=ips.read())!=-1){
ops.write(b ^ 0xff);
}
}
}
import java.util.Date;
public class ClassLoaderAttachment extends Date {
public String toString(){
return "Hello World";
}
}
一:运行MyClassLoader时,对ClassLoaderAttachment加密:
(1)首先,找到ClassLoaderAttachment的class文件,在bin目录下。
(2) 运行 MyClassLoader的时候:
注意下:这个时候 ClassLoaderAttachment 没有包名,在测试的时候 要把它放入包中,不然有异常:有包名的类不能调用无包名的类。
public class LoaderTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new ClassLoaderAttachment().toString());
}
}
此时 输出正确。
而这个时候,我们来对其加密:
(1)找到ClassLoaderAttachment的 Class文件。
(2)运行MyClassLoader:
这是 拿到 刚刚加密完的class文件 去覆盖掉 最早的那个好的文件:
覆盖好了以后,在运行loaderTest,就会报错: