mercredi 29 avril 2020

ClassNotFoundException while loading class from a java 9 module that was loaded at runtime

I'm getting a ClassNotFoundException when I'm trying to load a class using Class.forName() that was added to the JVM using a ModuleLayer.

This is the error message:

Exception in thread "main" dev.teamnight.nightweb.core.module.ModuleException: Module does not contain main class dev.teamnight.nightweb.Test.TestApp
        at dev.teamnight.nightweb.core/dev.teamnight.nightweb.core.module.JavaModuleLoader.loadModule(JavaModuleLoader.java:80)
        at dev.teamnight.nightweb.core/dev.teamnight.nightweb.core.module.ModuleManagerImpl.loadModule(ModuleManagerImpl.java:92)
        at dev.teamnight.nightweb.core/dev.teamnight.nightweb.core.module.ModuleManagerImpl.loadModules(ModuleManagerImpl.java:64)
        at dev.teamnight.nightweb.core/dev.teamnight.nightweb.core.impl.NightWebCoreImpl.<init>(NightWebCoreImpl.java:194)
        at dev.teamnight.nightweb.core/dev.teamnight.nightweb.core.Main.main(Main.java:21)
Caused by: java.lang.ClassNotFoundException: dev.teamnight.nightweb.Test.TestApp
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at java.base/jdk.internal.loader.Loader.loadClass(Loader.java:544)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:398)
        at dev.teamnight.nightweb.core/dev.teamnight.nightweb.core.module.JavaModuleLoader.loadModule(JavaModuleLoader.java:66)
        ... 4 more

I used the example from the ModuleLayer JavaDoc in order to load the class:

        ModuleMetaFile metaFile = this.getModuleMetaFile(path);

        ModuleFinder finder = ModuleFinder.of(path.toAbsolutePath());
        ModuleLayer parent = ModuleLayer.boot();
        Configuration conf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of(metaFile.getModuleIdentifier()));
        ClassLoader scl = ClassLoader.getSystemClassLoader();
        ModuleLayer layer = parent.defineModulesWithOneLoader(conf, scl);

        try {
            ClassLoader loader = layer.findLoader(metaFile.getModuleIdentifier());
            Class<?> clazz = Class.forName(metaFile.getMainClass(), true, loader);

            try {
                NightModule module = (NightModule) clazz.getConstructor().newInstance();

                return module;
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                throw new ModuleException(e, "Missing public " + clazz.getSimpleName() + "() constructor in " + metaFile.getMainClass());
            }
        } catch (ClassNotFoundException e) {
            throw new ModuleException(e, "Module does not contain main class " + metaFile.getMainClass());
        }
    }

The Module is loaded into the ModuleLayer as I checked by checking layer.modules(), but it is throwing the Exception above.





Aucun commentaire:

Enregistrer un commentaire