Minecraft Forge 1.16.5构建Jar包时Gradle依赖缺失问题
解决Minecraft Forge 1.16.5 Mod构建Jar后JSON-Simple依赖缺失问题
问题描述
下载1.16.5版本Forge MDK开发Mod,用IntelliJ IDEA的runClient启动游戏时Mod可正常运行,但构建Jar包后放入Minecraft启动,出现以下错误:
java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser at com.example.examplemod.JsonUtil.getConfig(JsonUtil.java:14)
build.gradle中已添加依赖配置:
dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'com.googlecode.json-simple:json-simple:1.1.1' }
尝试过Gradle Shadow插件(配置错误)、将implementation改为minecraftLibrary、完整声明依赖等方法,均未解决问题。
解决方案
方法1:正确配置Gradle Shadow插件(推荐)
Shadow插件可将第三方依赖打包进Mod Jar,彻底解决依赖缺失问题,修改build.gradle步骤如下:
- 在plugins块添加Shadow插件:
plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' }
- 替换原Jar任务配置,改用ShadowJar生成最终产物:
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") ]) } dependsOn 'reobfJar' archiveFileName.set("${mod_id}-${mod_version}.jar") } tasks.named('build').configure { dependsOn 'shadowJar' } publishing { publications { register('mavenJava', MavenPublication) { artifact shadowJar } } }
- 执行
./gradlew build,生成的Jar会包含JSON-Simple依赖,直接放入游戏mods文件夹即可。
方法2:使用include关键字强制打包依赖
若不想用Shadow插件,可修改dependencies块,添加include关键字告诉Forge Gradle打包指定依赖:
dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" include 'com.googlecode.json-simple:json-simple:1.1.1' implementation 'com.googlecode.json-simple:json-simple:1.1.1' }
执行./gradlew build重新生成Jar即可。
原理说明
- IDE的
runClient会自动加载项目所有依赖,因此本地运行无问题。 - 默认Forge Gradle构建任务仅打包Mod自身代码,不会包含
implementation声明的第三方依赖,导致游戏启动时找不到对应类。 include关键字可让Forge Gradle将指定依赖打包进最终Jar;Shadow插件则提供更强大的依赖合并、打包功能,适合复杂依赖场景。
内容的提问来源于stack exchange,提问作者vefixx
相关产品推荐
相关产品推荐

