在自定义类加载器上修复NoClassDefFoundError

时间:2018-11-29 19:24:32

标签: java

public class JarByteClassloader extends ClassLoader {
  public JarInputStream jis;
  public JarByteClassloader(JarInputStream jarInputStream) throws IOException {
    super(Thread.currentThread().getContextClassLoader());
    this.jis = jarInputStream;
  }
}

在这里使用哪个:

public void executeMainClass(CustomCodeRequest request) {
    String classToLoad = null;
    String methodName = request.getMethodName();
    final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try{
      Pair<String,JarInputStream> pair = extractMainClassManifest(jar);
      classToLoad = pair.getFirst();
      JarByteClassloader loader = new JarByteClassloader(pair.getSecond());
      Class c = loader.loadClass(classToLoad);
      Thread.currentThread().setContextClassLoader(loader);
      JarEntryObject jarEntry = (JarEntryObject) c.newInstance();
      List<JarCodeMethod> methods = jarEntry.methods();
      for (JarCodeMethod cc : methods) {
        // This code throws NoClassDefFoundError
        String ccMethodName = cc.getMethodName();
        if (methodName.equals(ccMethodName)) {

        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally{
      Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
  }

投掷:

Caused by: java.lang.NoClassDefFoundError: com/apex/backends/methods/ProcessBranch
    at com.apex.backends.MyJarEntryObject.methods(MyJarEntryObject.java:22)

NoClassDefFoundError的根本原因是什么?以及如何解决?

罐子在哪里

  

公共类MyJarEntryObject扩展JarEntryObject {@Override     公共列表方法(){列表   方法=新的LinkedList(); methods.add(新   ProcessMethod());返回方法; }

0 个答案:

没有答案