Bukkit - 无法在插件中使用外部 JAR

问题描述 投票:0回答:1

我目前正在为我的 Bukkit 服务器制作一个插件,但我无法使用我的外部库;因为服务器在加载它们时会抛出一个

NoClassDefFoundError
。这是因为我使用的外部库本身不是 Bukkit 插件。我真的需要这些库,因为没有它们,该插件根本无法工作。

库:Json Simple、Google GSON(用于漂亮的打印)、Apache Commons 和我的自定义 PacketBuffer 库。

java jar classpath minecraft bukkit
1个回答
-2
投票

Bukkit 内置了简单的 JSON 和 GSON,不确定 Apache Commons。 如果您想将库加载到插件/服务器,您可以使用反射“破解”它。

将库 jar 文件放置在插件可访问的地方,并使用

loadFile()
方法加载库文件。

public static void loadFile(File file) throws IOException {
    invokeLoader(file.toURI().toURL());
}

public static void invokeLoader(URL url) throws IOException {
    final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    final Class<?> sysclass = URLClassLoader.class;

    try {
        final Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });

        method.setAccessible(true);
        method.invoke(sysloader, new Object[] { url });
    } catch (Throwable t) {
        t.printStackTrace();

        throw new IOException("Could not add URL to system class loader!");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.