• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Referencing to image files

 
Dhiman Biswas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a images folder at the root of all packages and using user.dir system property to get the absolute path for images. This works fine only when run the app from package root directory. How can I reference the image file so that it always work, I mean when I run the app as a jar file?
 
Marco Barenkamp
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
First of all,
You should not use any property to get the location
of your image files.
Assuming an image is located in the folder '/images/star.gif',
you should code the following:
<pre>
java.net.URL res = this.getClass().getResource("/images/star.gif");
JLabel pic = new JLabel(new ImageIcon(res));
</pre>
This will always work, even if you're using jar archives.

------------------
 
Dhiman Biswas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was not aware of the getResource() method. It works.
Thank you very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic