本帖最后由 清心玉质 于 2013-8-30 08:38 编辑
package Jiami;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
class MyClassLoader extends ClassLoader
{
public static void main(String[] args) throws Exception
{
String srcPath = args[0];
String destDir = args[1];//目标的目录
FileInputStream fis = new FileInputStream(srcPath);//在这里,如果用绝对路径可以直接用,用相对路径需要计算,为了方便这里用了相对路径(在工程下面)
System.out.println(srcPath);
String destFileName = srcPath.substring(srcPath.lastIndexOf("\\")+1);//目标文件名
System.out.println(destFileName);
String destPath = destDir +"\\"+ destFileName;
System.out.println(destPath);
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);
}
}
private String classDir;
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// TODO Auto-generated method stub
System.out.println("name is"+name);
String classFileName = classDir + "\\"+name +".class";
System.out.println("classFileName is"+classFileName);
try {
FileInputStream fis = new FileInputStream(classFileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
cypher(fis,bos);
fis.close();
byte[] bytes = bos.toByteArray();
return defineClass(bytes,0,bytes.length);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public MyClassLoader(){
}
public MyClassLoader(String classDir){
this.classDir = classDir;
}
}
------------------------------------------------------------------------
/**
*
*/
package Jiami;
import java.util.Date;
/**
* @author Administrator
*
*/
public class ClassLoaderTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());
System.out.println(System.class.getClassLoader());
ClassLoader loader = ClassLoaderTest.class.getClassLoader();
while(loader !=null){
System.out.println(loader.getClass().getName());
loader = loader.getParent();
}
System.out.println(loader);
//System.out.println(new ClassLoaderAttachment().toString());
Class clazz = new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
Date d1 = (Date)clazz.newInstance();
System.out.println(d1);
}
}
------------------------------------------------------------------------------------------------
执行结果报错:
sun.misc.Launcher$AppClassLoader
null
sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$ExtClassLoader
null
Exception in thread "main" java.lang.NoClassDefFoundError: ClassLoaderAttachment (wrong name: Jiami/ClassLoaderAttachment)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Jiami.ClassLoaderTest.main(ClassLoaderTest.java:32)
|