Solved

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

Posted on 2002-05-27
13
810 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
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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 77
jsp insert to database example 2 47
SequenceInputStream example 3 19
passing enum to a method 4 23
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

830 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