Retrieving Images from a .jar File

I need to get the res folder for compilation, when I export the jar executable to eclipse, also when I use the method getClass().getResource(), it does not work.

The current image code to read

public Image loadImage(String fileName) {
    return new ImageIcon(fileName).getImage();
}

Code that doesn't work

public Image loadImage(String fileName) {
    return new ImageIcon(getClass().getResource(fileName).getImage();
}
+3
source share
2 answers

Now I fixed the problem - this is the code that works

 public BufferedImage loadImage(String fileName){

    BufferedImage buff = null;
    try {
        buff = ImageIO.read(getClass().getResourceAsStream(fileName));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    return buff;

}

The fileName value is simply the name of the image, for example, BufferedImage img = loadImage ("background.png");

Thank you all for your help.

+2
source

Or:

  • , , , , , , , , . , , , .

  • . jar zip- / zip , , .
+2

All Articles