0

Explanation: I'm currently programming game library in java with IntelliJ IDEA Community Edition. I'm using jSFML as a multimedia library. I tried rendering a sprite, using jsfml's wiki page, and everything seemed to work fine, so I moved on.

Problem: Later I tried compile the library with a testing game, and realized that sprite of player can't be loaded. When it's running from editor, everything is fine, but if I run it outside of editor, it's not working. It's giving me this error, what I believe means that file doesn't exist, i don't know:

java.io.IOException: Failed to load image "C:\src\main\resources\Player.png". Reason : Unable to open file
    at org.jsfml.graphics.Texture.loadFromFile(Unknown Source)
    at org.jsfml.graphics.Texture.loadFromFile(Unknown Source)
    at filipeex.gamer.library.core.GameRenderer.DisplaySprite(GameRenderer.java:32)
    at filipeex.gamer.library.gameobjects.Player.FrameRender(Player.java:38)
    at filipeex.gamer.library.core.Game.FrameRender(Game.java:118)
    at filipeex.gamer.library.core.Game.InitLoop(Game.java:206)
    at filipeex.gamer.library.core.Game.InitWindow(Game.java:180)
    at filipeex.gamer.library.core.Game.Start(Game.java:52)
    at filipeex.gamer.library.testing.TestingAsUser.main(TestingAsUser.java:29)

This is code for loading texture and rendering it to window:

GameRenderer.DisplaySprite("/src/main/resources/Player.png", position, rotation);

And the method itself:

Texture t = new Texture();
t.loadFromFile(Paths.get(path));

Sprite s = new Sprite(t);
s.setPosition(position.x, position.y);
s.setScale(1, 1);
s.setRotation(rotation);

GameWindow.Get().draw(s);

I've already:

  • searched for a lot of similar questions,
  • tried every possible solution from them, including adding directory to artifact, didn't work.
FilipeeX
  • 13
  • 2
  • Because the path you have is to the relative location in your uncompiled project!! `src\main\resources\Player.png`. You need to use the classloder. – Boris the Spider Sep 04 '21 at 07:37
  • You're calling `Texture.loadFromFile`. You don't want it to be from a file - you want it to be from a resource within the jar file. I suspect you want `loadFromStream`, having called `ClassLoader.getResourceAsStream` or similar. – Jon Skeet Sep 04 '21 at 07:40

0 Answers0