Solved

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

Posted on 2002-05-27
13
807 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 84
How do I remove an object from a 3 27
What's wrong with this code? 4 23
ejb on wildfly 5 20
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…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

803 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