Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-05-27
13
Medium Priority
?
816 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

715 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