1

I'm trying to make a jar, but the program needs images. When I ran the jar, the images didn't show up. However, they did in eclipse. I used this code:

label.setIcon(new ImageIcon("res/img/icon.png"));

Then I went looking on the internet for a way to fix it. I found this question here on StackOverflow, but when i tried it in my code, it's throwing a NullPointerException (also in eclipse). This is my code now:

label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));

The images are in a separate folder in the package, called "res":

enter image description here

Community
  • 1
  • 1
cvbattum
  • 811
  • 2
  • 15
  • 32
  • 2
    make sure the image is actually inside of the Jar, and that it's inside the res/img/ directory. Your syntax looks right – greedybuddha Jun 05 '13 at 17:46
  • where your `res` is? It must be under your classpath – AlexR Jun 05 '13 at 17:46
  • 1
    You should take a screenshot like this: http://stackoverflow.com/a/13695798/1834700 – Rong Nguyen Jun 05 '13 at 17:49
  • `NullPointerException` here means `.getResource()` returns `null`; and it does if the resource does not exist. Your image is not where you think it is... – fge Jun 05 '13 at 17:49
  • @RongNK Does it actually have to look like that (my project setup)? – cvbattum Jun 05 '13 at 17:51
  • @CaspervanBattum Of course not. We just want to see your project layout, since your given resource URL is evidently incorrect. – FThompson Jun 05 '13 at 17:52
  • @CaspervanBattum as a guess I'd say `res` is part of your project layout; try and remove `/res` in `.getResource()` – fge Jun 05 '13 at 17:54
  • Try cleaning and rebuilding your project. In Eclipse, open the Navigator view. Open the /bin folder of the project. Does it have the /res/img/icon.png or instead /ing/icon.png ? – Menelaos Jun 05 '13 at 17:55
  • The path is correct, I'm 100% sure of that (checked it 10 times) – cvbattum Jun 05 '13 at 17:57
  • 1
    @Casper van Battum oh man, you have incorrect path, you should move `res` folder to `src` folder now ! – Rong Nguyen Jun 05 '13 at 17:59
  • Thanks, @RongNK. That actually worked (in eclipse atleast, for some reason it refuses to create a jar out of it...). – cvbattum Jun 05 '13 at 18:03
  • Good, it's working now both in, and out of eclipse! Thanks! – cvbattum Jun 05 '13 at 18:09
  • 1
    You didn't have to move res inside src. You just should say to Eclipse that res is a source folder, and thus would be included in the jar (project properties > Java Build Path > Source tab > Add Folder). And in the tree view you'll see the folder icon will change to a folder with a package. – polypiel Jun 05 '13 at 19:00

2 Answers2

1
  1. create a package inside your project, and name it something like "Images".
  2. Now, add images you are using into this package.
  3. Finally, call to these images, which are inside the package.

make sure you typed image names correctly as well

That is the best way of dealing with images

update

try

label.setIcon(new ImageIcon(getClass().getResource("res/img/icon.png")));

instead of

label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
PeakGen
  • 21,894
  • 86
  • 261
  • 463
0

Have a look that your RES folder is actually within the eclipse SRC folder. Sometimes, users add a folder to the eclipse project instead.

Also:

  • Try cleaning and rebuilding your project.
  • In Eclipse, open the Navigator view. Open the /bin folder of the project. Does it have the /res/img/icon.png ( comment from here )
Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155