我想为自己编写的一个类指定自己的类加载器,结果出现运行异常?:
public class Student {//被加载的类
}
import java.io.*;
public class MyClassLoader extends ClassLoader {//定义自己的类加载器
public Class findClass(String name) {
FileInputStream fis;
byte[] b=new byte[1000];
try {
fis = new FileInputStream(name);
fis.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return defineClass(name, b, 0, b.length);
}
}
public class Test {//测试类
public static void main(String[] args) throws ClassNotFoundException {
ClassLoader cl=new MyClassLoader();
cl.loadClass("E:\\worksp7777\\1\\src\\a\\Student.java");
System.out.println(Student.class.getClassLoader());
}
}
运行log:
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1885430635 in class file E:\worksp7777\1\src\a\Student/java
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at a.MyClassLoader.findClass(MyClassLoader.java:16)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at a.Test.main(Test.java:7)
|
|