日期:2014-05-20  浏览次数:20891 次

学习自定义类加载器,运行时候报错,求解!!!
[code=Java][/code]<p>
import java.util.*;
 

public class ClassLoaderTest {
 

  public static void main(String[] args)throws Exception{
 
  Class clazz = new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
 
  Date d1 = (Date)clazz.newInstance();
 
  System.out.println(d1);
 

  }
 

}
 
import java.io.*;
 

public class MyClassLoader extends ClassLoader {
 

  public static void main(String[] args)throws Exception {
 
  String srcPath1 = args[0];
 
  String srcPath2= args[1];
 
  String destdir= args[2];
 
  String srcPath = srcPath1+' '+srcPath2;
 
  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 is,OutputStream os)throws Exception{
 
  int b = -1;
 
  while((b=is.read())!=-1){
 
  os.write(b^0xff); 
  }
 
  }
 
  private String classDir;
 
  @Override
 
  protected Class<?> findClass(String name) throws ClassNotFoundException {
 
  String classFileName = classDir + "\\" + name + ".class";
 
  try {
 
  FileInputStream fis = new FileInputStream(classFileName);
 
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
  cypher(fis,bos);
 
  fis.close();
 
  System.out.println("aaa");
 
  byte[] bytes = bos.toByteArray();
 
  return defineClass(bytes, 0, bytes.length);
 
  } catch (Exception e) {
 
  e.printStackTrace();
 
  }
 
   
   
  return null;
 
  }
 
   
 
  public MyClassLoader(){
 
   
 
  }
 
  public MyClassLoader(String classDir){
 
  this.classDir = classDir;
 
   
  }
 
   
 

}
 


Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 889275713 in class file <Unknown>

哪位高人指导下!!

------解决方案--------------------
os.write(b^0xff); 改为 os.write(b&0xff);