import java.util.Date;public class ClassLoaderAttachment extends Date{ //类一; 这个类是附件类
@Override
public String toString(){
return "hello! ClassLoaderAttachment!";
}
public static void main(String[] args){
System.out.println("main method :"+new ClassLoaderAttachment().toString());
}
public ClassLoaderAttachment(){
System.out.println("构造函数");
}}import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;public class MyClassLoader extends ClassLoader{ //类二 是自己写的类加载器
public MyClassLoader(){
}
public MyClassLoader(String classdir){
this.classdir=classdir;
} public static void main(String[] args)throws IOException {
String srcpath=args[0];
String destdir=args[1];
FileInputStream fis=new FileInputStream(srcpath);
String destFileName=srcpath.substring(srcpath.lastIndexOf("\\")+1);
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 IOException{
int b=-1;
while((b=ips.read())!=-1){
ops.write(b ^ 0xff);
}
}
/*
* 写一个自己加载类的方法,用来给其它类调用加载时用,
*/
private String classdir;
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException{
String classFileName=classdir+"\\"+name+".clss";
System.out.println(classFileName); //这里打印一下路径名只是为了验证一下是否正确
try {
FileInputStream fis = new FileInputStream(classFileName); //问题一: 这里的流就死活加载不进来了
ByteArrayOutputStream bos = new ByteArrayOutputStream();
cypher(fis, bos);
fis.close();
byte[] bytes = bos.toByteArray();
return defineClass(classFileName,bytes,0,bytes.length-1);
} catch (Exception e) {
System.out.println("这里的子类加载器出问题");
}
return super.findClass(classFileName);
}
}import java.util.Date;
import java.lang.Class;public class ClassLoaderTest { // 类三: 用于测试加载类是否写的成功 /**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args)throws Exception{
/*
* 用自己写的加载器来加载类
*/
//问题二: 下面这句代码执行有错误,老是报这样的异常
// Exception in thread "main"java.lang.ClassNotFoundException:itcastlib\ClassLoaderAttachment.clss Class<?> clazz= new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
Date d1=(Date)clazz.newInstance();
System.out.println(d1);
}
}问题是编译的时候还没有任何错误,苦苦不得结果,求高手解答
在这个项目的根目录下,有个文件夹itcastlib ,里面已经放进去了编译后的ClassLoaderAttachment.class
@Override
public String toString(){
return "hello! ClassLoaderAttachment!";
}
public static void main(String[] args){
System.out.println("main method :"+new ClassLoaderAttachment().toString());
}
public ClassLoaderAttachment(){
System.out.println("构造函数");
}}import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;public class MyClassLoader extends ClassLoader{ //类二 是自己写的类加载器
public MyClassLoader(){
}
public MyClassLoader(String classdir){
this.classdir=classdir;
} public static void main(String[] args)throws IOException {
String srcpath=args[0];
String destdir=args[1];
FileInputStream fis=new FileInputStream(srcpath);
String destFileName=srcpath.substring(srcpath.lastIndexOf("\\")+1);
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 IOException{
int b=-1;
while((b=ips.read())!=-1){
ops.write(b ^ 0xff);
}
}
/*
* 写一个自己加载类的方法,用来给其它类调用加载时用,
*/
private String classdir;
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException{
String classFileName=classdir+"\\"+name+".clss";
System.out.println(classFileName); //这里打印一下路径名只是为了验证一下是否正确
try {
FileInputStream fis = new FileInputStream(classFileName); //问题一: 这里的流就死活加载不进来了
ByteArrayOutputStream bos = new ByteArrayOutputStream();
cypher(fis, bos);
fis.close();
byte[] bytes = bos.toByteArray();
return defineClass(classFileName,bytes,0,bytes.length-1);
} catch (Exception e) {
System.out.println("这里的子类加载器出问题");
}
return super.findClass(classFileName);
}
}import java.util.Date;
import java.lang.Class;public class ClassLoaderTest { // 类三: 用于测试加载类是否写的成功 /**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args)throws Exception{
/*
* 用自己写的加载器来加载类
*/
//问题二: 下面这句代码执行有错误,老是报这样的异常
// Exception in thread "main"java.lang.ClassNotFoundException:itcastlib\ClassLoaderAttachment.clss Class<?> clazz= new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
Date d1=(Date)clazz.newInstance();
System.out.println(d1);
}
}问题是编译的时候还没有任何错误,苦苦不得结果,求高手解答
在这个项目的根目录下,有个文件夹itcastlib ,里面已经放进去了编译后的ClassLoaderAttachment.class
结果:构造函数
hello! ClassLoaderAttachment!
java.lang.ClassFormatError:
Incompatible magic value 889275713 in class file itcastlib\ClassLoaderAttachment/class
搞的头都大了.
File f = new File(classFileName);
f.exists(); //false