简体   繁体   中英

How do I use textures in a .jar?

I'm using LWJGL and Slick-Util. My texture loading works fine in Eclipse, but when I export it to a .jar, it gives java.lang.RuntimeException: Resource not found: res/test/texture.png

This is my texture loading code:

public int loadTexture(String file) {
    Texture tex = null;
    try {
        tex = TextureLoader.getTexture("PNG", new FileInputStream("res/" + file + ".png"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    int texID = tex.getTextureID();
    textures.add(texID);
    return texID;
}

You probably do not run the application from the right directory. For your code to work, you need to start the program in a directory containing the res folder.

Alternatively you can use

...
try {
    tex = TextureLoader.getTexture("PNG", this.getClass().getResourceAsStream("/res/" + file + ".png");
} catch (FileNotFoundException e) {
     ...
}

But in this case res folder needs to be added to classpath:

java -cp .;path/to/res;some.jar your.main.Class

使用OctoArcher摆脱getResource()/ getResourceAsStream()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM