0

Hai I have an application which is designed in netbeans6.0.1 where i need to display an image as an logo.My coding is as below

mLogo = new JLabel();
URL lUrlLogo = getclass().getResource("/com/images/image.gif");
Image lLogoimage = Toolkit.getDefaultToolkit().getImage(lUrlLogo);
ImageIcon licon = new ImageIcon(lLogoimage);
mLogo.setIcon(licon);

My code works fine when i execute it in netbeans but when I try to run my jar file which is created by netbeans in dist folder,it gives me an exception

Uncaught error fetching image:
java.lang.NullPointerException
        at sun.awt.image.URLImageSource.getConnection(Unknown Source)
        at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
        at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
        at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
        at sun.awt.image.ImageFetcher.run(Unknown Source)

Whats the problem with my jar Can some one help me to run my jar Thanking u

2 Answers 2

3

Your jar file will need to include the image.

See this tutorial or this one for instructions for how to do that. Basically if you include the file in a source folder (with the appropriate path) it should be okay.

If you think you've already got the image there, run

jar tvf file.jar

to show the contents. Make sure everything's where you expect it to be in terms of directories.


EDIT: As per the comments, the problem turned out to be due to case sensitivity.

Finding resources in a jar file is always case-sensitive, whereas loading them from a file system is only case-sensitive if the file system is. So if your file is image.GIF but you're looking for image.gif then it'll work when you're loading from the file system in Windows, but not in the jar file.

3
  • yes i have the image inside my jar and in the location where i need it
    – chaithu
    Sep 3, 2009 at 6:50
  • If you call getResourceAsStream() instead of getResource(), what happens? If that returns null, then it sounds like the file isn't there or in the right place. Do you have it correct in terms of casing? Even if your normal file system is case-insensitive, resource lookup in the jar file won't be.
    – Jon Skeet
    Sep 3, 2009 at 6:58
  • Thank u I got it its casesensitive with in extension i have changed it and its working
    – chaithu
    Sep 3, 2009 at 8:11
0

And you have checked that the JAR file contains the path "/com/images/image.gif"?

Also, I'm not really sure about the leading slash, I always used relative paths to access resources (but I guess it should work anyways).

As an example from class it.lapo.example.Main I use getClass().getResource("resources/logo.png") and in the JAR files are this way:

it/lapo/example/Main.class
it/lapo/example/resources/logo.png

I know... not much different from your own example, but it does work... so I guess yours is quite close to being correct too.

1
  • I have used relativepaths and I found my image in specified path but still its not working
    – chaithu
    Sep 3, 2009 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.