0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hasan05
  • 904
  • 5
  • 22
  • Possible duplicate of [Embedding resources (images, sound bits, etc) into a Java project then use those resources](https://stackoverflow.com/questions/3721706/embedding-resources-images-sound-bits-etc-into-a-java-project-then-use-those) – MC10 Sep 25 '17 at 17:24
  • Unzip the Jar file, see if the resources are been included. If not, you will need to figure out how to include them, which will be determine by how you are generating the Jar file in the first place – MadProgrammer Sep 25 '17 at 20:54

1 Answers1

0

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.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • `getClass().getResource` returns resources within the current class path - it has nothing to do with the `src` directory - some IDEs package resources which are stored in the `src` directory, but strip the `src` path element. *"Changes to only /image.png to load it from the project structure"* - based on the input, I'd expect it to be `/bin/image.png`, but without details about how the Jar is generated, it's impossible to be sure – MadProgrammer Sep 25 '17 at 20:53
  • The /src/ generates compiled class into /bin/ and the root of it will be '/' – Marcos Vasconcelos Sep 25 '17 at 21:19
  • Under what JVM/build system? `bin` is just a directory/package like any other, depending on how the system is achieved, it's just as easily to be `/bin/...` or something other directory - assuming the default operation of most IDEs `/bin/...` is a safer bet - unless the OP is prepared to provide more details about how they package the Jar - everything is just assumption and conjecture – MadProgrammer Sep 25 '17 at 21:52