在java中使用不安全类加载动态类时发生ClassFormatError
当我运行应用程序时,我得到了运行时的在java中使用不安全类加载动态类时发生ClassFormatError,java,mongodb,Java,Mongodb,当我运行应用程序时,我得到了运行时的ClassFormatError。 像 示例代码: ObjectTransformer ot = null; try{ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new Exception("JDK required (running inside
ClassFormatError
。
像
示例代码:
ObjectTransformer ot = null;
try{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new Exception("JDK required (running inside of JRE)");
}
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fm = compiler.getStandardFileManager(diagnostics, null, null);
final JavaFileManager javaFileManager = new ForwardingJavaFileManager(fm) {
@Override
public JavaFileObject getJavaFileForOutput(Location location,
String className,
JavaFileObject.Kind kind,FileObject sibling) throws IOException {
return simpleJavaFileObject;
}
};
ToolProvider.getSystemJavaCompiler().getTask(null, javaFileManager, null, null, null, singletonList(simpleJavaFileObject)).call();
final byte[] bytes = baos.toByteArray();
boolean classLoaded = true;
Class<?> aClass = null;
try{
aClass = Class.forName(className);
}catch(ClassNotFoundException cnfe){
classLoaded = false;
}
if(!classLoaded){
final Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
final Unsafe unsafe = (Unsafe) f.get(null);
aClass = unsafe.defineClass(className, bytes, 0, bytes.length);
}
ot = (ObjectTransformer) aClass.newInstance();
}catch (Exception e){
System.out.println("Error "+e.getMessage());
e.printStackTrace();
}
ObjectTransformer ot=null;
试一试{
JavaCompiler=ToolProvider.getSystemJavaCompiler();
if(编译器==null){
抛出新异常(“需要JDK(在JRE内部运行)”;
}
诊断收集器诊断=
新诊断收集器();
StandardJavaFileManager fm=compiler.getStandardFileManager(诊断,null,null);
final JavaFileManager JavaFileManager=新转发JavaFileManager(fm){
@凌驾
公共JavaFileObject getJavaFileForOutput(位置,
字符串类名称,
JavaFileObject.Kind(类,FileObject同级)引发IOException{
返回simpleJavaFileObject;
}
};
getTask(null,javaFileManager,null,null,null,singletonList(simpleJavaFileObject)).call();
最终字节[]字节=bas.toByteArray();
布尔类加载=真;
类aClass=null;
试一试{
aClass=Class.forName(className);
}捕获(ClassNotFoundException cnfe){
classLoaded=false;
}
如果(!classLoaded){
最后一个字段f=Unsafe.class.getDeclaredField(“theUnsafe”);
f、 setAccessible(true);
最终不安全=(不安全)f.get(null);
aClass=unsafe.defineClass(类名,字节,0,字节.长度);
}
ot=(ObjectTransformer)aClass.newInstance();
}捕获(例外e){
System.out.println(“错误”+e.getMessage());
e、 printStackTrace();
}
如何解决这个问题。。任何人都知道这一点,请与我分享知识。我有一个图书馆为你做这件事。默认情况下完全在内存中工作。但是,当我得到系统Java编译器时。当我得到JDK required(而不是JRE)异常时。。我已经设置了构建路径。是的,您需要在JDK中运行来编译代码,您使用的是哪个版本的Java?Java 1.7版(JDK 1.7)-Peter Lawreyan,您肯定是从JDK而不是JRE运行的?
ObjectTransformer ot = null;
try{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new Exception("JDK required (running inside of JRE)");
}
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fm = compiler.getStandardFileManager(diagnostics, null, null);
final JavaFileManager javaFileManager = new ForwardingJavaFileManager(fm) {
@Override
public JavaFileObject getJavaFileForOutput(Location location,
String className,
JavaFileObject.Kind kind,FileObject sibling) throws IOException {
return simpleJavaFileObject;
}
};
ToolProvider.getSystemJavaCompiler().getTask(null, javaFileManager, null, null, null, singletonList(simpleJavaFileObject)).call();
final byte[] bytes = baos.toByteArray();
boolean classLoaded = true;
Class<?> aClass = null;
try{
aClass = Class.forName(className);
}catch(ClassNotFoundException cnfe){
classLoaded = false;
}
if(!classLoaded){
final Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
final Unsafe unsafe = (Unsafe) f.get(null);
aClass = unsafe.defineClass(className, bytes, 0, bytes.length);
}
ot = (ObjectTransformer) aClass.newInstance();
}catch (Exception e){
System.out.println("Error "+e.getMessage());
e.printStackTrace();
}