File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes null ImageIcon from getResource in jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null ImageIcon from getResource in jar" Watch "null ImageIcon from getResource in jar" New topic
Author

null ImageIcon from getResource in jar

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I have the following code snippet:

From eclipse, this runs fine and returns the icon. I don't reach "past new icon" when I am executing the jar file, and the imageURL is null.

My class is in "durz.images"; I tried it once with the images in that same directory and just specifying iconName; I tried moving the images to their own "images" subdirectory and specifying "images/" + iconName.

I have verified that the jar file contains the images in the directory path, relative to the root of the jar file, that they're supposed to be in. I did that once when they were in the same directory as the class, and again after I moved them to their own images directory off the directory the class was in.

I am aware that the "/" at the front of the getResource() string parameter means "absolute path within the jar", and without the "/" the path is relative to the path the class is in, in the jar.

Can anyone see what I'm doing wrong here?

rc
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3378
    
    9
Hi Ralph. Can you post an SSCCE?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Ralph Cook wrote:From eclipse, this runs fine and returns the icon. I don't reach "past new icon" when I am executing the jar file, and the imageURL is null.


How, exactly, did you run the program outside of the IDE? Command line? what command? or did you just double-click an executable jar?


luck, db
There are no new questions, but there may be new answers.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Darryl Burke wrote:
Ralph Cook wrote:From eclipse, this runs fine and returns the icon. I don't reach "past new icon" when I am executing the jar file, and the imageURL is null.


How, exactly, did you run the program outside of the IDE? Command line? what command? or did you just double-click an executable jar?


I put it in c:\local, started a dos prompt window, changed my default directory to c:\local, and entered "java -jar durz.jar". The program ran ok, but blew up when I hit this code.

rc
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
SSCE helps again; I'm on Windows, and in eclipse, case is not significant on the resource file. It is significant, however, in the jar. So the case of the resource filename must be the same as stored in the jar.

thanks!
rc
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3378
    
    9
Wow really? That's an eye-opener for me as well.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479


If you put "Expanded.GIF" in a sub-directory named "images", it works from a jar unless/until you change the capitalization on the filename.

It makes sense; the zip file format has to be usable across OSs, so it has to handle (and distinguish among) different cases of filename.

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null ImageIcon from getResource in jar
 
Similar Threads
putting image files to Jar File in Swing application
problem in running swings app from command line
Loading Images
Problems loading images from application JAR
How can I find my images??