我想加载一个罐子里的类。
我已经尝试使用以下代码加载该类:
URL dirUrl = jarFile.toURL();
URLClassLoader ucl = new URLClassLoader(new URL[] {dirUrl}, getClass().getClassLoader());
Class<?> clazz = ucl.loadClass(mainClass);
ucl.close();
我收到一个找不到该类的异常:
java.lang.RuntimeException: java.lang.ClassNotFoundException: net.api.main
at net.al.Main.onEnable(Main.java:39) ~[?:?]
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_211]
Caused by: java.lang.ClassNotFoundException: net.api.main
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_211]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_211]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_211]
at net.al.addon.AddonLoader.loadAddons(AddonLoader.java:129) ~[?:?]
at net.al.Main.onEnable(Main.java:36) ~[?:?]
... 16 more
答案 0 :(得分:0)
请检查net.api.main是否存在
或检查变量jarFile和mainClass
答案 1 :(得分:0)
我通过将jar添加到类路径来修复它。 代码到文件到jar的路径:
public static void addURLToClassPath(URL url) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
{
URLClassLoader classLoader = (URLClassLoader) MyClass.class.getClassLoader();
@SuppressWarnings("rawtypes")
Class clazz = URLClassLoader.class;
@SuppressWarnings("unchecked")
Method method = clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(classLoader, new Object[] { url });
}