【问题标题】:ClassDefNoFound even though the jar that I'm dependent on is in my jarClassDefNoFound 即使我依赖的罐子在我的罐子里
【发布时间】:2020-03-31 10:27:39
【问题描述】:

我正在编写一个 LabyMod 插件来为我的世界客户创建一个商店机器人。

我已经成功地做到了,至少在我的 Eclipse 开发环境中。现在我想发布它,为此,LabyMod 提供了一种可能性,可以使用 gradle 将与插件相关的所有内容打包到插件 jar 中,然后可以简单地将其拖到“插件”文件夹中。

但是,当我这样做时,插件并没有出现在插件概述中,我发现加载插件时出错(未找到Class def)。我使用okhttp3向我的验证服务器发送一些网络请求,所以我只是将它作为依赖jar添加到eclipse中,但是在gradle构建jar时它们没有包含在内。所以我对此并不感到惊讶,并寻找将我的依赖项直接添加到 jar 的可能性。

我在预定义的build.gradle 的评论中发现只需将所有文件放在一个预定义的“libs”文件夹中,这似乎很好。这就是问题所在:尽管罐子肯定是罐子的一部分,但 classdefnotfound-thing 仍然会发生。这就是我的意思。

JD-GUI 谈到我的 jar:

所以,jar 肯定在那里,jd-gui 也可以在代码中找到它们,因为您可以单击导入并转到 okhttp3 定义导入类的位置。

这是我在尝试加载插件时遇到的错误:

catching
java.lang.NoClassDefFoundError: okhttp3/OkHttpClient$Builder
    at borg.locutus.grieferbot.utils.NetworkUtilities.sendGet(NetworkUtilities.java:70)
    at borg.locutus.grieferbot.utils.NetworkUtilities.getPermissionLevelOfPlayer(NetworkUtilities.java:51)
    at borg.locutus.grieferbot.GrieferBOTAddon.initPermissionLevel(GrieferBOTAddon.java:50)
    at borg.locutus.grieferbot.GrieferBOTAddon.onEnable(GrieferBOTAddon.java:43)
    at net.labymod.addon.AddonLoader.enableAddon(AddonLoader.java:349)
    at net.labymod.addon.AddonLoader.enableAddons(AddonLoader.java:309)
    at net.labymod.main.LabyMod.init(LabyMod.java:256)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:560)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
    at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
    at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
    at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
    at com.google.common.eventbus.EventBus.post(EventBus.java:275)
    at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:211)
    at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:189)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
    at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
    at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
    at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
    at com.google.common.eventbus.EventBus.post(EventBus.java:275)
    at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118)
    at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:737)
    at net.minecraftforge.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:310)
    at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:495)
    at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:329)
    at net.minecraft.client.main.Main.main(SourceFile:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient$Builder
    at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:191)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 44 more
Caused by: java.lang.NullPointerException
    at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:182)
    ... 46 more

如何让我的 jar 找到缺少的依赖项?

【问题讨论】:

  • 您不能只是将 jar 文件放在另一个 jar 中并让它开箱即用。您要么必须对它们进行遮蔽(创建“fat”或“über”jar),要么将它们放在应用程序旁边并在启动时配置类路径。对于前者,请查看 Gradle 的 shadow plugin

标签: java gradle dependencies minecraft minecraft-forge


【解决方案1】:

您不能只将依赖项 jar 放入主 jar。您需要将依赖项隐藏到它们中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2012-09-03
    • 1970-01-01
    • 2013-04-10
    • 2014-05-08
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多