使用java

时间:2017-01-03 13:01:29

标签: java mongodb

当我运行我的应用程序时,那时我得到ClassFormatError运行时。 像,

 at sun.misc.Unsafe.defineClass(Native Method)
       at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
       at sun.reflect.MethodAccessorGenerator+1.run(MethodAccessorGenerator.java:381)

示例代码:

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

如何解决这个......任何人都知道这一点,请与我分享知识..

0 个答案:

没有答案