LibGDX游戏jPackage打包后运行报错:Child process exited with code 1
问题:jPackage生成的LibGDX游戏EXE运行报错
Child process exited with code 1 我使用jPackage将LibGDX游戏转换为可执行文件,遵循教程操作时jpackageImage任务执行正常,但生成的exe运行时提示错误:Child process exited with code 1。环境为Windows 11系统、JDK 18.0.1.1,已正确配置JAVA_HOME与PATH,同时使用Badass-Runtime插件,desktop模块的build.gradle文件如下:
plugins { id 'org.beryx.runtime' version '1.8.4' } sourceCompatibility = 1.7 sourceSets.main.java.srcDirs = ["src/"] sourceSets.main.resources.srcDirs = ["../core/assets"] mainClassName = "com.mygdx.game.desktop.DesktopLauncher" def osName = System.getProperty('os.name').toLowerCase(Locale.ROOT) project.ext.assetsDir = new File("../core/assets") task runGame(dependsOn: classes, type: JavaExec) { main = project.mainClassName classpath = sourceSets.main.runtimeClasspath standardInput = System.in workingDir = project.assetsDir ignoreExitValue = true } task debug(dependsOn: classes, type: JavaExec) { main = project.mainClassName classpath = sourceSets.main.runtimeClasspath standardInput = System.in workingDir = project.assetsDir ignoreExitValue = true debug = true } task dist(type: Jar) { manifest { attributes 'Main-Class': project.mainClassName } dependsOn configurations.runtimeClasspath from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } with jar destinationDirectory = file("$buildDir/lib") } jpackageImage.dependsOn dist dist.dependsOn classes eclipse.project.name = appName + "-desktop" runtime { options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages', '--strip-native-commands', '--vm', 'server'] modules = ['java.base', 'java.desktop', 'jdk.unsupported'] distDir = file(buildDir) jpackage { jpackageHome = 'C:\\Users\\pc\\.jdks\\openjdk-18.0.1.1' mainJar = dist.archiveFileName.get() if (osName.contains('windows')) { imageOptions = ["--icon", file("../icons/icon.ico")] } else if (osName.contains('linux')) { imageOptions = ["--icon", file("../icons/icon.png")] } else if (osName.contains('mac')) { imageOptions = ["--icon", file("../icons/icon.icns")] } } }
排查与解决步骤
- 检查资源文件路径:打包后程序的工作目录与开发环境不同,需确保assets目录被正确打包到程序目录,或在代码中动态获取资源路径:
修改DesktopLauncher中的资源加载逻辑,通过Jar包路径定位assets:File jarPath = new File(DesktopLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile(); File assetsDir = new File(jarPath, "assets"); new LwjglApplication(new MyGdxGame(), new LwjglApplicationConfiguration(), assetsDir); - 添加缺失的JDK模块:LibGDX可能依赖更多JDK模块,当前配置的
modules集合可补充java.logging、java.xml等,或临时使用--add-modules=ALL-MODULE-PATH自动检测依赖(会增大包体积,仅用于排查)。 - 启用控制台查看详细日志:在Windows平台的jpackage配置中添加
--win-console参数,运行exe时会弹出控制台,显示具体错误信息:if (osName.contains('windows')) { imageOptions = ["--icon", file("../icons/icon.ico"), "--win-console"] } - 验证Jar包可用性:运行
java -jar build/lib/your-game.jar测试dist任务生成的Jar包是否能正常启动,排除Jar包本身的依赖或配置问题。 - 升级Badass-Runtime插件:当前使用的1.8.4版本较旧,尝试升级到最新稳定版,新版本可能修复了jpackage相关的兼容问题。
内容的提问来源于stack exchange,提问作者eisa.exe
相关产品推荐
相关产品推荐

