Solved

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

Posted on 2002-05-27
13
811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 85
ejb entity bean example issue 2 43
running on tomcat not jboss eap 7.0 3 52
Coding for the first time 9 100
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

734 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