Java – Add image to JAR Java


I'm using some images in JFrame, I have given a path to load image into panel of the frame, but when i made jar of the application its not displaying images.
Where should I place the image?
How should I specify the path?


like this i have done.

Best Solution

First step: Put your image in a defined location in your JAR-file. If you put it into the src-folder, maybe your IDE or your build-tool will package it to the JAR automatically. Otherwise check the documentation of your IDE/build-tool, in which location you have to put it.

Second step: Access the file from your program. I assume you put it in the package (the path in your JAR) package1/package2 and the file is called dump.jpg. In that case you call:


getClass().getResource(...) returns an URL for a resource on your classpath. You start the path in the classpath with a '/' and use the complete path/packages to your resource, separated by '/'.