0

Note: I did try out ImageIcon background = new ImageIcon(getClass().getResource("MyImage.jpg")); mentioned in all the answers.

My project works fine using netbeans IDE run. But once I export to an executable jar using 'Clean and build', A null pointer exception is thrown and the frame with the image will not even open. (The executabe jar is in the 'dist' folder)

Even after using relative path, the image will never load.

  1. I put the image in same directory as the executable jar.(dist)
  2. I put the image in the lib folder. (dist\lib)
  3. I created an src and res folders and put image in those folders.(dist\src & dist\res)

None of the above worked.

I opened the jar with winRar and saw the image is present. but it never loads on running. Can anyone please help me.??

user2670866
  • 88
  • 2
  • 14

1 Answers1

0

Finally I figured it out.

ImageIcon background = new ImageIcon(getClass().getClassLoader().getResource("MyImage.jpg"));

Some how class loader plays a huge role !!

Also it is important to get file name exactly. .JPG is run in the IDE but on exporting to a JAR, It must be name .jpg itself if we are trying to access MyImage.jpg

user2670866
  • 88
  • 2
  • 14