javaeclipselibgdxexecutable-jargradle-eclipse

Executable jar with libgdx: asset not found


I created a little Game using the libgdx libraries and exported it to an executable jar. In Eclipse the Game works fine but the jar crashes with:

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

The problematic line is:

private final Sprite player_down =
    new Sprite(new Texture("img/player/player_16_down.png"));

I followed the libgdx wiki to create the jar. The assets folder is included in the build path and the content of the assets folder is in the exported jar:

I also tried using Gdx.files.internal (also, removing final)

private final Sprite player_down = new Sprite(new Texture(Gdx.files.internal("img/player/player_16_down.png")));

but there was no difference.

I am able to export the basic project, which exists when a new gradle project is imported into eclipse as an executable jar (without errors when executing the jar).

Similar questions I found had the problem that the assets folder was not included in the jar, which doesn't seem to be a problem here.

Any ideas?


Solution

  • Just place all content of your assets folder to the same directory as jar in.