I have been all over the internet trying to work out how to get an image icon displayed after compiling into a runnable jar. I discovered this problem way too late, I ran my
With your current directory setup, the images
dir won't even get built into the jar. Try to extract it and you will most likely see that it's not in there.
You can tell by the fact that it doesn't have the little package logo in the folder, as seen here with resources
The only default directory built into the classpath (/jar) is the src
. We need to either put the resources
into the src
or configure the build path to include the files that are in the resources
. Once we do that, we will see the little package icon inside the folder icon. That's how we know the files are on the build path
First Image: Can't, it won't work (this your current predicament)
Second Image:
ImageIcon icon = new ImageIcon(
getClass().getResource("/resources/stackoverflow.png"));
Third Image:
ImageIcon icon = new ImageIcon(
getClass().getResource("/stackoverflow.png"));
To configure the build path to use the third option, follow the instructions in Example 2 in this answer
As from your screenshot, the image exists in a folder called "images". Put it in a folder inside your classpath: src/images/success.jpg and call:
ImageIcon a = new ImageIcon(getClass().getResource("/images/success.jpg"));