Solved

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

Posted on 2002-05-27
13
803 Views
Last Modified: 2013-11-23
 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
Comment
Question by:JavaSlave
  • 3
  • 3
  • 3
  • +2
13 Comments
 

Expert Comment

by:Kit_
ID: 7038344

Try the following

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

Expert Comment

by:s_lavie
ID: 7038697
Or:
ImageIcon icon = new ImageIcon(((URLClassLoader)getClass().getClassLoader()).findResource("image/a.gif"));
0
 
LVL 92

Expert Comment

by:objects
ID: 7038770
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
 
LVL 2

Expert Comment

by:s_lavie
ID: 7038848
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
 
LVL 92

Expert Comment

by:objects
ID: 7038859
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
 

Author Comment

by:JavaSlave
ID: 7039569
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:s_lavie
ID: 7039583
Do you include the jar file in your classpath?
0
 
LVL 92

Expert Comment

by:objects
ID: 7040544
Try:

new ImageIcon(a.class.getResource("/image/a.gif"))
0
 

Accepted Solution

by:
Kit_ earned 50 total points
ID: 7040882
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
 

Author Comment

by:JavaSlave
ID: 7041653
Thank Kit, :-) the path is case-sensitive?
0
 

Expert Comment

by:Kit_
ID: 7041667
Not at all :)
I think, yes. But not sure.
0
 

Author Comment

by:JavaSlave
ID: 7043487
It's ok. Thank you.
0
 
LVL 1

Expert Comment

by:skorpiun
ID: 8720972
I had the same problem, Kit's suggestion worked great,.. thanks Kit
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
changeXy challenge 13 56
topping3 challenge 14 49
Eclipse IDE - Cannot copy/paste from console output 8 62
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now