getClass().getResource() error from jar file.
I am using getClass().getResource("../images/image.gif") to refer to a resource and load it into the ImageIcon. So that my resource's url is dynamic and be got at runtime. It is working fine when I run my program, but when I exported my application into a jar file then while trying to execute the jar file using the
java -jar program.jar command it is giving the following error (Stack trace)
Exception in thread"main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at source.HashCalculator.<init>(HashCalculator.java:130)
at source.HashCalculator.main(HashCalculator.java:2021)
# 1
Why should so be happening? I am unable to understand it, please help...
# 2
It is hard to know without seeing your code, but perhaps you are passing a null argument to the ImageIcon constructor.
# 3
Have you double checked that if, for example, you are accessing this from the class "com.foo.Bar" you have a file in the Jar at "com/images/image.gif"...?ie, start at the package where your class is, go up one folder, down into images, and there should be your file.
# 4
I am using the following code for loading my image:
private final ImageIcon aboutGif=new ImageIcon(getClass().getResource("../images/about.gif"));
Yes I am sure that it is the right path and when I run my program then it is running fine and it is loading the images also perfectly.
The problem is when I am executing an exported jar file at that time it is giving this error, why should this be?
# 5
Well, the only explanation is that the URL returned by getResource() is null, which mean the file doesn't exist at that location.I'd look in that Jar again.
# 6
Well see my directory structure is like this
/images/all_images.gif
/sources/myprogram.class
now I am calling the ImageIcon as I have mentioned before...
While running it from the eclipse there are no problems, but when I export a jar file and then try executing it, then it's giving me that error.
Why should this be? Where is the mistake? :(
Thanks & Regards.
Dhruva Sagar.
# 7
Well see my directory structure is like this
Within the Jar?!
That implies you have a class called "myprogram" in a package called "sources." Which sounds unlikely.
I suspect what's happening is that you have a class called "myprogram" in the root package and whatever build script you are using is packaging the contents of the "images" and "sources" folder into the Jar root.
I also suspect you're looking at your filesystem and not your Jar, despite me having told you to look in the Jar twice.
Try the resource path "about.gif"
# 8
Dude, that is my directory structure within the jar file!!!
I understand the difference between the two...
I opened the jar file using winrar and saw it.
Yes my myProgram.class file is in the source package and hence in the source folder.
Does it make things more clear to you?
# 9
My apologies then. You have an unorthodox package name which looks suspiciously like a resource structure instead of a package structure, that's all.
I assume you have an image called "about.gif" rather than "all_images.gif"
But if it's all as you suggest then it will work, so I'm still reckoning that something is getting lost in your vague description.
You say you're using this line for loading the image,
private final ImageIcon aboutGif=new ImageIcon(getClass().getResource("../images/about.gif"));
What does the "package" statement say at the top of the source file?
If that line is in a class called "myclass" which starts with the line "package source;" and your Jar includes the files "/source/myclass.class" and "/images/about.gif" then I'm stumped.
# 10
Well nice to hear that you are stumped as well :)).
Well you are absolutely right, i have quite a strange package name that I have used, infact that name eclipse also uses to differentiate between source files and binary files if one wants...
But in my class file i have the statement package source in the first line as you think. And yes my images...like about.gif are also in the images/about.gif folder.
This directory structure is the same in both the jar file and on my system. And yes I am stumped as well...
But still, hoping to get a better reply from some one...
# 11
Is my problem not clear enough, or really nobody is able to identify with it?There must be something wrong somewhere...
# 12
Class.getResource() probably doesn't handle ".." notation when referring to entries in a JAR file. Try getResource("/images/about.gif").
# 13
The solution to this error is to include in jar the directory where the *gif is located ex: image/image.gif .... jar cmf yourmanifestfile yourjarfile image
# 14
The solution to this error is to include in jar the location of the *gif file(ex: yourpath/images/yourimage.gif) ..... jar cmf yourmanifest yourjar yourclass yourpath/images