用defineClass报错问题 求教

cllncjh 2013-02-28 02:33:58

E:Plug.jar 是一个单独的Plug.class (没上层包)打的jar

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class CompilingClassLoader extends ClassLoader {
public Class getClas(String filename,String classname) throws IOException {
File file = new File(filename);
try
{ System.out.println("be to the Cload");
System.out.println("classname:"+classname);
Class ctmp = this.findLoadedClass(classname);
System.out.println(ctmp.getName()+" is load");
System.out.println("over!!");
return ctmp;
}
catch(Exception exception)
{
System.out.println("memory is null");
}
long len = file.length();
byte[] raw = new byte[(int)len];
FileInputStream fis = new FileInputStream(file);
int r = fis.read(raw);
if(r!=len) throw new IOException ("Cat not read all"+r+"!="+len);
fis.close();
return defineClass("classname",raw,0,raw.length);
}

public static void main(String[] args) throws IOException {
String s = "E:/Plug.jar";
File file = new File(s);
CompilingClassLoader ccl = new CompilingClassLoader();
Class clas = ccl.getClas(s,"Plug.class");
}

}


Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1347093252 in class file classname
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.lang.ClassLoader.defineClass(ClassLoader.java:466)
at CompilingClassLoader.getClas(CompilingClassLoader.java:28)
at CompilingClassLoader.main(CompilingClassLoader.java:35)
...全文
292 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cllncjh 2013-02-28
  • 打赏
  • 举报
回复
JDK是1.6版的
lvzg_005 2013-02-28
  • 打赏
  • 举报
回复
jdk版本不一致
lvzg_005 2013-02-28
  • 打赏
  • 举报
回复
jar包编译用的jdk版本和这个class的版本是多少
cllncjh 2013-02-28
  • 打赏
  • 举报
回复
有人帮帮吗 新人求教啊 第一次发帖 答了就结

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧