Search code examples
javaimageswingjframeembedded-resource

Image not showing in JFrame - Java Application


For add an image, I used this code.

JLabel label = new JLabel(new ImageIcon(getClass().getResource("/bin/image.png")));

image location: Application/src/bin/image.png.

Problem is, In my computer debugging mode it shows the image. But after clean and built project if I copy the folder (Application/dist) to another computer it does not show the image. How can I show the image in other computers?


Solution

  • The getClass().getResource returnas a file from within the /src/ package and will be bundled in the bin after build.

    Changes to only /image.png to load it from the project structure

    Else if you want to copy the resources (or add new items trough source) you must use the full qualified resource path name and load it as a File instead.

    Also, you need to export with resources trough Eclipse or FatJar, dont know trough Ant.

    I did also had some issues by getting resources with getClass().getResource, instead I did use getClass().getClassLoader().getResource.