简体   繁体   中英

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.

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

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

Check out my old code, this might even work

ByteBuffer buf = null;
    try{
    texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/" + key + ".png"));
    int texId = texture.getTextureID();
    buf = ByteBuffer.allocateDirect(4*texture.getImageWidth()*texture.getImageHeight());
    }catch(IOException e){
        e.printStackTrace();
    }   

    // Create a new texture object in memory and bind it
    //int texId = GL11.glGenTextures(); - if the trick with texture.getTextureID() won't work - use THIS
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);

    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, texture.getImageWidth(), texture.getImageHeight(), 0,
    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);

    //then you can generate some mipmaps

    //Setup the ST coordinate system
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_LINEAR);

And the thing you need is the texId .

I hope you know how to work with textures ;).

What I did was I had a folder called source in netbeans project and when i made a .jar I used it with JarSplice to load the lib and native. Then I opened the new .jar with winrar and I dragged the source into the .jar and every time you run the .jar it will use the folder inside the jar. source is the same thing like res but i renamed it.

So basically put your res folder into .jar and it will work.

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