首页 > 试题广场 >

TypeA.class位于classpath下, abso

[问答题]
TypeA.class位于classpath下, /absolute_path/TypeA.class为其在文件系统中的绝对路径, 且类文件小于1k, MyClassLoader为一个自定义的类加载器, 下面的这段类加载程序是否正确, 如果有错请指出哪一行有错, 简述理由。
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Tester{
public static void main(String[] args){
   MyClassLoader cl1=new MyClassLoader();
   try{
    File f=new File("/absolute_path/TypeA.class");
    byte[] b=new byte[1024];
    InputStream is=new FileInputStream(f);
    int I=is.read(b);
    Class c=cl1.defineMyClass(null,b,0,1);
    TypeA a=(TypeA)c.newInstance();
   }catch(Exception e){
    e.printStacktrace();
   }
  }
}
    Class c=cl1.defineMyClass(null,b,0,1);
这一行错误了吧,这里只是查询该类是否已存在定义,而不是加载操作,应该调用loadClass这个方法加载

发表于 2017-11-14 23:47:40 回复(1)