java.lang.ClassNotFoundException 尝试加载派生时 class

java.lang.ClassNotFoundException when trying to load derived class

我有两个不同的模块:MultiRealmSurvivalRealm。 MultiRealm 提供了一个抽象 class (LoadedRealm),class SurvivalRealm(在模块 SurvivalRealm 中)对其进行了扩展。现在,MultiRealm 尝试从 jar 加载 SurvivalRealm class。但是在尝试加载时,出现以下错误: java.lang.NoClassDefFoundError: net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm 其中 net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm 是 LoadedRealm class 的正确位置。这是堆栈跟踪:

[23:03:52] [Thread-10/WARN]:    at java.lang.ClassLoader.defineClass1(Native Method)
[23:03:52] [Thread-10/WARN]:    at java.lang.ClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.security.SecureClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader.access0(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader.run(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader.run(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.security.AccessController.doPrivileged(Native Method)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader.findClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm.RealmFactory(LoadedRealm.java:54)
[23:03:52] [Thread-10/WARN]:    at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm.RealmFactory(LoadedRealm.java:20)
[23:03:52] [Thread-10/WARN]:    at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.Initialize(RealmManager.java:22)
[23:03:52] [Thread-10/WARN]:    at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.lambda$InitializeAsync[=12=](RealmManager.java:39)
[23:03:52] [Thread-10/WARN]:    at java.lang.Thread.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: Caused by: java.lang.ClassNotFoundException: net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader.findClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]:    ... 16 more

这里是 SurvivalRealm class:

import net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm;
import net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.Worlds.RealmWorld;

public class SurvivalRealm extends LoadedRealm {

    public SurvivalRealm(String RealmPath) {
        super(RealmPath);
        Load();
    }

    @Override
    public void Load() {
        //LoadStuff
    }

    @Override
    public void Unload() {

    }
}

... LoadedRealm 中的这个静态函数试图导入这个 class:

public static LoadedRealm RealmFactory(String jarPath, String RealmPath)  throws RealmLoadException{
        try {
            File file = new File(jarPath);
            URLClassLoader c = new URLClassLoader(new URL[]{file.getAbsoluteFile().toURI().toURL()});
            Class<?> realmClass = c.loadClass(RealmClass);
            return (LoadedRealm)(realmClass.getDeclaredConstructor(String.class).newInstance(RealmPath));
        }catch (Exception e){
            throw new RealmLoadException(e);
        }
    }

这是结构图:

MultiRealm 模块不导入/“了解”(请原谅我的术语)SurvivalRealm 模块,只是相反。

我只是想实现不可能的事情吗?如果您需要 .iml 文件,请询问。任何帮助都会非常感激,我现在​​基本上一整天都在努力解决这个问题。 感谢您阅读这个初学者问题。

您应该尝试使用 maven 中的 pom 文件来实现您的目标。多模块项目由引用一个或多个子模块的父 POM 定义。