Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue while loading images packaged in jar

 
Bhakta Pradhan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
pic.JPG
[Thumbnail for pic.JPG]
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Class.getResource or ClassLoader.getResource. Both return a URL that you can use directly with ImageIcon.
 
Bhakta Pradhan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does getSystemResource do? And why did you choose that method instead of what Rob suggested in his post?
 
Bhakta Pradhan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Please suggest me what shall I do?

open the apidocs for Class
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thans Rob I was able to load the images.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic