Search code examples
javatextureslwjglslick2d

In exported jar file textures are not loaded


I am using LWJGL and Slick2D. I export project to .jar and use JarSplice to generate executable, so I don't need library files in the location where executable jar is. My problem is that if I run project from Eclipse then every image is loaded, but if I run exported executable jar, image is not loaded, because it cannot be found. Pictures are in /res folder and this is method for loading textures:

private Texture loadTexture(String key) {
    try {
        return TextureLoader.getTexture("PNG", new FileInputStream(
                new File("res/" + key + ".png")));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

and here I load the texture:

Texture background = loadTexture("main_menu/bg");

I tried lot of ways exporting jar, but I don't get any working way.


Solution

  • You might need to load them as class path resources if they are within a jar. See:

     getClass().getClassLoader().getResourceAsStream(...)