Executable jar with libgdx: asset not found
I created a small game using libgdx libraries and exported it to an executable jar. In Eclipse, the game runs fine but crashes:
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
Problem line:
private final Sprite player_down =
new Sprite(new Texture("img/player/player_16_down.png"));
I created a libgdx wiki to create a jar. The assets folder is included in the build path, and the content of the assets folder is in the exported jar:
- / Com
- / IMG
- / javazoom
- / META_INF
- /net
- / org tile
- ...
I also tried using Gdx.files.internal
(also by 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 can export a base project that exists when a new gradle project is imported into eclipse as a jar executable (no jar execution errors).
Similar questions I found were related to the assets folder not being included in the jar, which doesn't seem to be the issue.
Any ideas?
source to share
I loaded the assets a little differently, my jar executable is working fine. Here's my Assets class:
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();
}
}
If you are not using a texture packer then you need to! If you are interested in checking HERE
source to share
I had the same problem as you. I also used Eclipse to generate jar files, and no matter what I did, the files were not found when I ran the jar file.
Solution :
-
First go to your project directory on the command line.
-
Run the command
gradlew desktop:dist
. -
The generated jar file can be found in
desktop/build/libs
.
Note. ... Another problem I found out about is that when you run your project in Eclipse, it does not respect file registration. This means that in Eclipse the project is being loaded into a file img.png
in code, but in fact img.png
, you won't get an error. However, when you run the jar file, you will get an error.
I hope this answer helped you, and if you have any further questions, feel free to leave comments below!
source to share