File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problems loading images from application JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems loading images from application JAR" Watch "Problems loading images from application JAR" New topic
Author

Problems loading images from application JAR

Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Sep 20, 2005
Posts: 862
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?


bitguru blog
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems loading images from application JAR
 
Similar Threads
images in jar file
Loading an Image
Adding Image, Bolds to JTextPane
Creating a Self-Contained Jar
Couldn't find file "C:\...\Images\"