【问题标题】:Executable jar with libgdx: asset not found带有 libgdx 的可执行 jar:未找到资产
【发布时间】:2014-08-24 16:56:20
【问题描述】:

我使用 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"));

我跟随 the 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 中的问题,这在这里似乎不是问题。

有什么想法吗?

【问题讨论】:

  • 您生成的 JAR 中是否有“资产”目录?我找不到证明这一点的代码,但我相信清单希望 JAR 中存在一个“资产”目录,其中包含资产(在你的情况下是“资产/img/”。)
  • 没有“资产”目录,但 jar 中也没有可以工作的目录。

标签: java eclipse libgdx executable-jar gradle-eclipse


【解决方案1】:

只需将你的assets文件夹的所有内容放到jar所在的同一目录即可。

【讨论】:

  • 这行得通,但不是那么漂亮(恕我直言)。有没有办法把所有东西都放进罐子里?
  • @RoHo 我找到了解决这个问题的方法。我假设您正在使用 Gradle,但如果没有,那些会遇到这个令人沮丧的问题的人将从 2018 年开始使用它。我找到的解决方案按照您的要求将所有资产打包到 jar 中。
【解决方案2】:

我加载资产有点不同,我的可执行 jar 文件运行良好。这是我的资产类:

public abstract class Assets {
private static AssetManager asm = new AssetManager();
private static String assetsPackPath = "assets.pack";
public static TextureAtlas atlas;

public static void load(){
    asm.load(assetsPackPath, TextureAtlas.class);
    asm.finishLoading();

    atlas = asm.get(assetsPackPath);
}

public static void dispose(){
    asm.dispose();
}
}

如果您不使用纹理打包程序,那么您应该使用!有兴趣请查看HERE

【讨论】:

  • 我知道我应该使用纹理打包器并将检查您的解决方案。到目前为止,我只想得到一些又快又脏的东西,然后再清理。我仍然需要自己研究纹理打包器和图集,并在完成后添加评论。
【解决方案3】:

我遇到了与您不久前相同的问题。我也在使用 Eclipse 生成我的 jar 文件,无论我做什么,运行 jar 文件时都找不到这些文件。

解决方案

  • 首先在命令行中导航到您的项目目录。

  • 运行命令gradlew desktop:dist

  • 生成的jar文件可以在desktop/build/libs找到。

注意: 我发现的另一个问题是,当您在 Eclipse 中运行项目时,文件扩展名不区分大小写。这意味着在 Eclipse 中,一个项目在代码中加载文件img.png,但实际上是img.PNG,你不会得到错误。 但是,运行 jar 文件时会报错

我希望这个答案对您有所帮助,如果您有任何其他问题,请随时在下面发表评论!

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2012-03-15
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多