3

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:

  • /com
  • /img
  • /javazoom
  • /META_INF
  • /net
  • /org
  • /tiles
  • ...

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?

msrd0
  • 7,816
  • 9
  • 47
  • 82
RoHo
  • 33
  • 1
  • 6
  • Is there an "assets" directory in your generated JAR? I can't find the code to prove this, but I believe the manifest expects an "assets" directory to exist in the JAR with the assets in it (so "assets/img/" in your case.) – P.T. Aug 24 '14 at 18:19
  • There is no "assets" dir, but there is also none in the jar that works. – RoHo Aug 24 '14 at 18:29

3 Answers3

0

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

Metaphore
  • 749
  • 1
  • 7
  • 15
  • 1
    That works but it's not that pretty (imho). Is there a way to get everything into the jar? – RoHo Aug 24 '14 at 21:12
  • @RoHo I found a solution to this problem. I assume that you are using Gradle but if not, those who will get this frustrating problem will be using it as of 2018. The solution I found packages all assets into the jar as you have asked. – Micheal O'Dwyer Jan 24 '18 at 15:10
0

I loaded assets a bit differently, my executable jar file runs 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 texture packer then you should! If interested check HERE

Community
  • 1
  • 1
lxknvlk
  • 2,744
  • 1
  • 27
  • 32
  • I know I should use the texture packer and will check your solution out. So far I just wanted to get something _quick and dirty_ and clean up later. I still need to work myself into the texture packer and atlas and will add a comment when I've done that. – RoHo Aug 25 '14 at 09:24
0

I had the very same issue that you have a while ago. I was also using Eclipse to generate my jar files and no matter what I did the files were not found when I ran the jar file.

The solution:

  • First navigate to the directory of your project in the command-line.

  • Run the command gradlew desktop:dist.

  • The generated jar file can be found in desktop/build/libs.

Note: Another issue that I found out about is that when you run your project in Eclipse, it is not case-sensitive about file extensions. This means that in Eclipse a project that loads in the file img.png in code, but is actually img.PNG, you will not get an error. However, you will get an error when you run the jar file.

I hope this answer helped you and if you have any further questions please feel free to post a comment below!

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26