Wolf
Wolf

Reputation: 3

exporting runnable jar with eclipse, want to use image files

I am working on a java project and I want to export it to a runnable jar file. This project has a GUI with several background images, and when I run it in eclipse with the images in my project folder it works, but when I try to export it to a jar, I get the following exception when running on cmd: javax.imageio.IIOException: can't read input file!

I have tried moving my images to various places in the project to attempt to include them in the jar, and even then it didn't work. I'm using File objects with commands like

final Image backgroundImage = javax.imageio.ImageIO.read(new File("background.png"));

How do I make it so that I can still use the images (preferably changing as little code as possible)?

Upvotes: 0

Views: 179

Answers (1)

Nick
Nick

Reputation: 487

Assuming that your image file, background.png, is in the same folder as your class file, replace new File("background.png") with this.getClass().getResource("background.png").

Upvotes: 1

Related Questions