Search code examples
javajarlwjgl

Compiling a java program to a jar errors in eclipse


I have created a Java program. It it is a game made with LWJGL. I have decided to import some textures with the built in Texture class from LWJGL (LWJGL is Light Weight Java Graphics Library with opengl). In Eclipse I have put src/imagename.png. It works in Eclipse. I export it into a jar file and run it (After putting required libraries and natives) and it does not run and can't find the file. So, I tried removing that and putting imagename.png. That didn't work either. I tried putting /imagename.png and it still didn't work. What should I do?

try {
        player = TextureLoader.getTexture("PNG", new FileInputStream(new File("src/player.png")));
    } catch (IOException e) {
        e.printStackTrace();
    }

Solution

  • Class.getResourceAsStream() or ClassLoader.getResourceAsStream() are the proper ways to get files from the Classpath, which is what you should be trying to do if you want to load your files from inside of a jar.