How to pack class and resource(image) in a execute-Jar file?

 I want to pack all my class files and its resource within a execute Jar file. But my program can't access the resource file(image) in the same file.

ImageIcon icon = new ImageIcon("image\\a.gif");

  The image folder is same with my class file package folder. "com". It works properly if I don't put those in a jar file. Please tell me how to solve the problem. Thank you.
JavaSlaveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Kit_Connect With a Mentor Commented:
If you has your image placed at image/a.gif, then

Icon icon = new ImageIcon(YourClass.class.getResource("/image/a.gif"))


If your image placed at com/java/image/a.gif, then

Icon icon = new ImageIcon(YourClass.class.getResource("image/a.gif"))


I think you have the first case.
0
 
Kit_Commented:

Try the following

Icon icon = new ImageIcon(YourClass.class.getResource("image/a.gif"))
0
 
s_lavieCommented:
Or:
ImageIcon icon = new ImageIcon(((URLClassLoader)getClass().getClassLoader()).findResource("image/a.gif"));
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
objectsCommented:
You'd be better off not casting the class loader instance as the class loader may not necessarily be a URLClassLoader.

The getClassLoader() method may in fact return null in some instances so using the Class's getResource() method is probably a bit safer.

Another option is to use the system class loader:

ClassLoader.getSystemResource(resoucename);
0
 
s_lavieCommented:
objects,
> ... class loader may not necessarily be
a URLClassLoader.
I agree, but
> The getClassLoader() method may in fact return null in some instances
Can you give an example?
0
 
objectsCommented:
From the javadoc for getClassLoader:

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
0
 
JavaSlaveAuthor Commented:
Hi everybody,
  Thank for all of your comments. But I still can't see the image in my program.

  My Jar file contains 2 folder :
com/java/a.class
image/a.gif

  All method above I have tried. Hmmm... There has something I miss? Please give me more comments. Thank you.
0
 
s_lavieCommented:
Do you include the jar file in your classpath?
0
 
objectsCommented:
Try:

new ImageIcon(a.class.getResource("/image/a.gif"))
0
 
JavaSlaveAuthor Commented:
Thank Kit, :-) the path is case-sensitive?
0
 
Kit_Commented:
Not at all :)
I think, yes. But not sure.
0
 
JavaSlaveAuthor Commented:
It's ok. Thank you.
0
 
skorpiunCommented:
I had the same problem, Kit's suggestion worked great,.. thanks Kit
0
All Courses

From novice to tech pro — start learning today.