我使用 libgdx 库创建了一个小游戏并将其导出到可执行 jar。在 Eclipse 中,游戏运行良好,但 jar 崩溃:
PS E:\Workspaces\JavaGames\Executable Jars> java -jar Pk2.jar
SetToNewScreen called
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: img/player/player_16_down.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at org.hofrob.entities.Player.<init>(Player.java:29)
at org.hofrob.screens.misc.Play.show(Play.java:121)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at org.hofrob.screens.Screenmanager.setToNewScreen(Screenmanager.java:63)
at org.hofrob.pkgame.MyPkGame.create(MyPkGame.java:20)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: img\player\player_16_down.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:220)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
... 12 more
有问题的行是:
private final Sprite player_down =
new Sprite(new Texture("img/player/player_16_down.png"));
我按照libgdx wiki创建了 jar。assets 文件夹包含在构建路径中,assets 文件夹的内容在导出的 jar 中:
- /com
- /img
- /javazoom
- /META_INF
- /网
- /org
- /瓷砖
- ...
我也尝试过使用Gdx.files.internal
(也删除final)
private final Sprite player_down = new Sprite(new Texture(Gdx.files.internal("img/player/player_16_down.png")));
但没有区别。
我能够导出基本项目,该项目在将新的 gradle 项目作为可执行 jar 导入 eclipse 时存在(执行 jar 时没有错误)。
我发现的类似问题有资产文件夹未包含在 jar 中的问题,这在这里似乎不是问题。
有任何想法吗?