本帖最后由 林豪 于 2012-5-26 17:06 编辑
- <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>
求解!!! |
|