我写了一个了加载器并一个class放在F盘,但当类加载进来时用newInstance()实现类的字节码时会抛出异常求高手指点哪里错了。
import java.io.*;
import java.lang.reflect.*;
class myclassloader extends ClassLoader
{
String path;
myclassloader()
{
super();
}
myclassloader(String path)
{
this.path=path;
}
@Override
@SuppressWarnings("deprecation")
protected Class findClass(String name)throws ClassNotFoundException
{
System.out.println("aaa");
FileInputStream read=null;
byte[] data=new byte[1024*64];
int number;
System.out.println("aaa");
try
{
System.out.println("aaa");
read=new FileInputStream(path+File.separator+name);
number=read.read(data);
System.out.println(number);
return defineClass(data,0,number);
}
catch (Exception e)
{
}
return super.findClass(name);
}
}
class adjust_father extends adjust
{}
class adjust
{
public adjust()
{
System.out.println("over");
}
}
public class classadd_text
{
public static void main(String[] args)throws Exception
{
Class a=new myclassloader("f:").loadClass("adjust.class");
System.out.println(a);
adjust aa=(adjust)a.newInstance();
System.out.println("aaa");
System.out.println(aa);
}
}
|
|