• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 826
  • Last Modified:

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.
0
JavaSlave
Asked:
JavaSlave
  • 3
  • 3
  • 3
  • +2
1 Solution
 
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
Kit_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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now