2014年5月17日星期六

Learn custom class loader , run time error, solved ! ! !

 


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

Which expert guidance ! !
------ Solution ---------------------------------------- ----
os.write (b ^ 0xff); instead os.write (b & 0xff);
------ Solution ------- -------------------------------------
========== ===== following passage to is.read (byte []), read and write, it should also, save converted ==================
int b = -1;

while ((b = is.read ())! = -1) {

os.write (b ^ 0xff);
}
------ For reference only ----------------------------- ----------
this does not need to change it, I've put him XOR byte code , and then how to change the print back
------ For reference only - -------------------------------------




that is true, good magic ah , but that is why

没有评论:

发表评论