javaurlclassloader

java.lang.ClassNotFoundException when trying to load derived class


I have two different Modules: MultiRealm and SurvivalRealm. MultiRealm provides an abstract class (LoadedRealm) which the class SurvivalRealm (in the Module SurvivalRealm) extends. Now, MultiRealm tries to load the SurvivalRealm class from the jar. But when trying to load, i get the Following error: java.lang.NoClassDefFoundError: net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm where net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm is the correct location of the LoadedRealm class. Here is the Stacktrace:

[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.access$100(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader$1.run(Unknown Source)
[23:03:52] [Thread-10/WARN]:    at java.net.URLClassLoader$1.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$0(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

Here is the 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() {

    }
}

... and this static function in LoadedRealm tries to import this 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);
        }
    }

Here is a picture of the Structure: enter image description here

The MultiRealm Module does not import/"Know about" (excuse my terminology) the SurvivalRealm module, only the other way around.

Am I just trying to achieve something impossible? If you need the .iml file, just ask. Any help would be really apreciated, I have been trying to fix this issue for basically the whole day now. Thanks for reading through this beginner issue.


Solution

  • You should try using your pom files from maven for achieve your goal. A multi-module project is defined by a parent POM referencing one or more submodules.