Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems loading images from application JAR

 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Swing application that uses some images and icons. When I run it from an exploded directory, everything works fine. But when I run the exact same code from a jar, the ImageIcon objects do not have an image. The images are being found in the JAR (see logging output below) by the call to getResource(), but loading them fails. The code, logging output, and jar structure is shown below.

The code I am using is right out of the Java Tutorial's How to Use Icons. And that the tutorial explicitly states that the getResouce() will work for images contained in the application jar file; which it is. But the ImageIcon constructor, which I believe uses MediaTracker behind the scenes, is not loading the image. I really don't know where to go from here. Any help is appreciated.



Logging Output when in exploded directory, and loading image is successful:



Logging Output when in jar, and loading image is unsuccessful:


JAR Structure:

[ May 13, 2008: Message edited by: Mark Vedder ]
 
Brian Cole
Author
Ranch Hand
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Vedder:
I have a Swing application that uses some images and icons. When I run it from an exploded directory, everything works fine. But when I run the exact same code from a jar, the ImageIcon objects do not have an image. The images are being found in the JAR (see logging output below) by the call to getResource(), but loading them fails.


Could the images be corrupted in the JAR? Can you see them
in a web browser after manually extracting them from the JAR?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:


Could the images be corrupted in the JAR? Can you see them
in a web browser after manually extracting them from the JAR?


Nice call Brian.

It does indeed seem that the images are corrupt in the JAR. Upon extracting the JAR, I could not view them in a browser nor an image editor. Then I looked at them closer, and their files sizes did not match the originals. So now I need to determine what is happening in the Ant build that is corrupting the images. I have a thought as to what it may be, and will look into it.

Thanks again! I never even thought to check the actual images themselves. I just assumed there was an issue with the code. D'Oh! A nice reminder of not jumping to conclusions when debugging
[ May 14, 2008: Message edited by: Mark Vedder ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic