【发布时间】:2020-04-30 00:05:01
【问题描述】:
当我尝试使用 Class.forName() 加载使用 ModuleLayer 添加到 JVM 的类时遇到 ClassNotFoundException。
这是错误信息:
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
我使用 ModuleLayer JavaDoc 中的示例来加载类:
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());
}
}
当我通过检查layer.modules() 进行检查时,模块已加载到 ModuleLayer 中,但它抛出了上面的异常。
【问题讨论】:
标签: java class reflection module classnotfoundexception