wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Issue while loading images packaged in jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issue while loading images packaged in jar" Watch "Issue while loading images packaged in jar" New topic
Author

Issue while loading images packaged in jar

Bhakta Pradhan
Greenhorn

Joined: Nov 26, 2010
Posts: 22
Hi,
I have used icons for buttons in my applications.Below code is able to load images when it is run from IDE(eclipse). But when I package everything in a jar file the images are not getting loaded.
Find the attachment to view the jar contents. Can you please let me know what should I do to load those images packaged inside jar file?
I'm using the below code.

where createMenuButton is a static method

where loadFile method is

where image path is defined as


[Thumbnail for pic.JPG]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Use Class.getResource or ClassLoader.getResource. Both return a URL that you can use directly with ImageIcon.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bhakta Pradhan
Greenhorn

Joined: Nov 26, 2010
Posts: 22
Hi Rob,
Thanks for your response. I changed the code as below.But My program is getting terminated without any error or warning message. Using println statement I found that it's getting terminated once it's trying to execute ClassLoader.getSyatemResource() Please let me know if I'm doing something wrong?

where iconPath is string(image file name eg. "Monitoring.png")

NB:- above code is present inside static method
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What does getSystemResource do? And why did you choose that method instead of what Rob suggested in his post?
Bhakta Pradhan
Greenhorn

Joined: Nov 26, 2010
Posts: 22
Hi Paul,
Thanks for looking into my issue.Actually I couldn't find ClassLoader.getResource() but I found ClassLoader.getSystemResource(). Hence I used the later. Please suggest me what shall I do?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Please suggest me what shall I do?

open the apidocs for Class
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Bhakta Pradhan wrote:Please suggest me what shall I do?


I always use Class.getResource for this, and that was one of Rob's suggestions.
Bhakta Pradhan
Greenhorn

Joined: Nov 26, 2010
Posts: 22
Hi I modified my code still images are not getting loaded.


it's not able to find the image .I have also tried only image name getResource("deploy.png")
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
in mthe Swing FAQ you'll find an example of a background image in a panel,
which uses similar code.

add a button to the panel (remove the paintComponent bit - meaning you'll have to handle the 'catch')
use the image to set the button's icon
add the panel to a frame
compile/run, if image displays OK, .jar it
run the .jar, see how you go
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

When you use Class.getResource, the resource is taken relative to the current class. To use an absolute resource, it must start with a "/". Note that ClassLoader.getResource always uses absolute resource names that should not start with a "/".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

As a follow-up from your other thread, to let Eclipse put the images / resources folder on your class path it should be a source path.
Bhakta Pradhan
Greenhorn

Joined: Nov 26, 2010
Posts: 22
Thans Rob I was able to load the images.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You're welcome.
 
wood burning stoves
 
subject: Issue while loading images packaged in jar