经典指数          
原因
1222
浏览数
0
收藏数
 

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(); } } }

     举报   纠错  
 
切换
1 个答案
Class c=cl1.defineMyClass(null,b,0,1); 这一行错误了吧,这里只是查询该类是否已存在定义,而不是加载操作,应该调用loadClass这个方法加载
 
切换
撰写答案