Rohit Elayathu
Rohit Elayathu

Reputation: 639

Images not shown while running the JAR created by Eclipse

I have my images folder inside source folder in my Eclipse IDE

After exporting the application into a runnable JAR, I run it through command prompt, but it does not display any images that I have used. When I run it through Eclipse, images are displayed.

And when I copy the source/images folder to where my executable JAR is, then it shows images, but I want my images also to be in JAR itself.

What is the problem?

Upvotes: 0

Views: 599

Answers (2)

Himanshu Mohta
Himanshu Mohta

Reputation: 1043

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;

}

Upvotes: 3

brimborium
brimborium

Reputation: 9512

Your images should NOT be in your source folder. You should hold them separate in another folder (assets or ressources or images or so).

To include the folder in your jar, I highly suggest using ANT script (or maven) to generate your jar. Its much more flexible. It takes a bit to get the grasp on it, but there are a lot of examples and tutorials around and I think its definitely worth it.

Upvotes: 1

Related Questions