classloader—使用另一个java应用程序获取和调用java应用程序的主类

qltillow  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(259)

我正在尝试用模块核心加载“inventorymodule”的主类。
每次我尝试加载主类(我成功地从模块的jar的信息文件中读取了主类)时,都会显示classnotfoundexception,因此不会加载模块。
这就是我试图查找和加载主类的方式(lobbymoduleconfig类中唯一的东西就是到主类的路径)

@SneakyThrows
    private Optional<Class<?>> loadClass(JarFile file) {
        JarEntry entry = file.getJarEntry("module.json");
        @Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(entry)));

        LobbyModuleConfig config = LobbyCore.instance().gson().fromJson(reader, LobbyModuleConfig.class);

        file.close();

        return Optional.ofNullable(Class.forName(config.mainClass()));
    }

例外情况如下:(

java.lang.ClassNotFoundException: net.bungeecordplugin.bwworld.lobby.module.inventory.Module
        at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_201]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_201]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_201]
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_201]
        at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_201]
        at net.bungeecordplugin.bwworld.lobby.core.module.loader.ModuleLoader.findMainClass(ModuleLoader.java:54) ~[?:?]
        at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source) ~[?:1.8.0_201]
        at java.util.Iterator.forEachRemaining(Unknown Source) ~[?:1.8.0_201]
        at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.AbstractPipeline.copyInto(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.AbstractPipeline.evaluate(Unknown Source) ~[?:1.8.0_201]
        at java.util.stream.ReferencePipeline.collect(Unknown Source) ~[?:1.8.0_201]
        at net.bungeecordplugin.bwworld.lobby.core.module.loader.ModuleLoader.loadModules(ModuleLoader.java:37) ~[?:?]
        at net.bungeecordplugin.bwworld.lobby.core.LobbyCore.enable(LobbyCore.java:26) ~[?:?]
        at net.bungeecordplugin.bwworld.lobby.core.plugin.LobbyCorePlugin.onEnable(LobbyCorePlugin.java:32) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题