ClassCastException on custom class loading

∥☆過路亽.° 提交于 2019-12-25 19:04:11

问题


I'm trying to write a scripting system in Java and I've managed to get my scripts to compile and instantiate but when I try to cast the script into a "DeftScript" it throws a ClassCastError even thought the script itself extends the class "DeftScript"

Error (the important part at least):

java.lang.ClassCastException: scripts.Compass cannot be cast to com.deft.core.scripts.DeftScript
    at com.deft.core.scripts.DeftScriptManager.instantiate(DeftScriptManager.java:52) ~[?:?]

The error is caused by this

deftScript = (DeftScript)obj;

Compiling and Instantiating:

public static DeftScript instantiate(String java) {
    File file = new File("./plugins/Deft-Core/scripts/" + java);

    DeftScript deftScript = null;

    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(file));
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit);
    if (task.call()) {

        Object obj = null;
        try {
            String jarFile = "./plugins/Deft-Core.jar";
            URLClassLoader classLoader = new URLClassLoader (new URL[] {new File(jarFile).toURI().toURL(), new File("./plugins/Deft-Core/").toURI().toURL()}, Thread.currentThread().getContextClassLoader());

            Class<?> loadedClass;
            loadedClass = Class.forName("scripts.Compass", false, classLoader);
            obj = loadedClass.newInstance();

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | MalformedURLException e) {
            e.printStackTrace();
        } 

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

    return deftScript;
}

Calling the instantiate method:

String script = "Compass.java";
DeftScriptManager.instantiate(script);

DeftScript.java

package com.deft.core.scripts;

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

Compass.java

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

回答1:


If your default classloader loads the class DeftScript and the .jar you are loading also contains the class DeftScript, then Java will think that these are two different classes with the same binary name but loaded by different classloaders and you will get that exception since Java sees you trying to mix the two different classes like they are the same thing.

The unique identification of a class in Java consists of the binary class name AND the classloader which was used to load the class.

If you create your URLClassloader like this :

URLClassLoader classLoader = 
    new URLClassLoader (new URL[] {new File(jarFile).toURI().toURL(), 
    new File("./plugins/Deft-Core/").toURI().toURL()},
           Thread.currentThread().getContextClassLoader());

The second parameter tells java to use the current thread's classloader first to load classes, and only load them from the jar in your URLClassLoader if they are NOT defined in the parent.

Now the classloader will refer to it's parent first and the class DeftScript will only be loaded by the parent classloader even though your .jar file defines the same class (by name).

This was a pretty good article describing the way that this works :

http://www2.sys-con.com/ITSG/virtualcd/java/archives/0808/chaudhri/index.html

This was also helpful

http://www.javaworld.com/article/2077344/core-java/find-a-way-out-of-the-classloader-maze.html?page=1



来源:https://stackoverflow.com/questions/31282985/classcastexception-on-custom-class-loading

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