NoClassDefFoundError ClassLoader

别说谁变了你拦得住时间么 提交于 2019-12-25 19:02:02

问题


Im currently getting an error on thie line loadedClass = classLoader.loadClass("scripts.Compass");

Somehow the file compiles fine but when I try to load the class it wont work because it says its missing the class com/deft/core/scripts/DeftScript

Compiling & Instantiating:

    File script = new File("./plugins/Deft-Core/scripts/Compass.java");
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

    List<String> optionList = new ArrayList<String>();
    optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/Deft-Core.jar"));


    Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(script));
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit);
    if (task.call()) {

        Object obj = null;
        try {
            URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./plugins/Deft-Core/").toURI().toURL()});
            Class<?> loadedClass;
            loadedClass = classLoader.loadClass("scripts.Compass");
            obj = loadedClass.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | MalformedURLException e) {
            e.printStackTrace();
        } 

        if (obj instanceof DeftScript) {
            DeftScript deftScript = (DeftScript)obj;
            deftScript.onEnable();
        }
    } else {
        for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
            System.out.format("Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri());
        }
    }

Compass.java

package scripts;
import com.deft.core.scripts.DeftScript;
public class Compass extends DeftScript {
    @Override
    public void onEnable() {
    }
}

DeftScript.java

package com.deft.core.scripts;

public abstract class DeftScript {
     public abstract void onEnable();
}

Error:

java.lang.NoClassDefFoundError: com/deft/core/scripts/DeftScript
    at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_45]
    at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
    at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_45]
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_45]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
    at com.deft.core.main.DeftCore.onEnable(DeftCore.java:79) ~[?:?]

回答1:


You need to load com.deft.core.scripts.DeftScript first before you load scripts.Compass as it is extending it. Or you can make sure you drop the parent class/jar in parent class loader path.



来源:https://stackoverflow.com/questions/31282240/noclassdeffounderror-classloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!