Reputation: 95
I have a Java project in Eclipse. This is the folder structure:
The code packages are located in src, and all images are in the res folder.
Images in the code are referenced like this:
new ImageIcon("res/settings.gif");
However, after I export the project to a JAR file (by using Export -> Runnable JAR File in Eclipse), and run the JAR, no images are shown. If I open the JAR with WinRAR, this is its structure:
All images are in the root folder, and can't be references correctly. How do I properly reference the images so that they are visible both from Eclipse and from the exported JAR?
Upvotes: 1
Views: 1504
Reputation: 95
I think I found the fix:
Replace this
new ImageIcon("res/settings.gif");
with
new ImageIcon(this.getClass().getResource("/settings.gif"));
Works both from Eclipse and from a runnable JAR file.
Upvotes: 0
Reputation: 829
The res/
folder is not created in the .jar file. It looks that the resource location should not be "res/settings.gif"
but "/settings.gif"
.
You are getting it to work if you debug in Eclipse because the home directory would be the root of your project, where the res/
folder actually exists.
Upvotes: 1
Reputation: 109613
The solution would be either to use an absolute path
new ImageIcon("/settings.gif");
or ensure that the res
folder is copied into the jar. Maybe src/res or mark the folder as source folder in eclipse; I now am in another IDE.
Then it would be
new ImageIcon("/res/settings.gif");
Upvotes: 0