• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading images in executable jar

 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have extended my GUI with a toolbar, using some icons for the major operations (very hard to find nice icons for connecting to local/remote database !!! any tips ???).
I placed the icons in a directory /images in the project root.
I attach the image to an Action using the following statement:


This works OK when i run my client from the commandline (or from JBuilder), using:
java -cp classes suncertify.client.swingview.RunClient

But after "jarring" my client, the images are not found anymore !
When i start the client using:
java -jar FBNClient.jar
the client runs well but the images are not shown. My jarfile contains the following directories in its root:
/META-INF
/suncertify
/images

So my question is: how does the ImageIcon constructor resolve the relative path to the imagerfile, and how can it be set so that it finds the image within the jarfile ?
I hope for some tips...
Regards, Klaas
 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code appears to work, i found an example in a newsgroup.
Running the executable Jarfile goes OK, when i start the client using the non-packaged version, i need to add the current dir to the classpath:
java -cp .;classes suncertify.client.swingview.RunClient



I do not completely understand the code. The "this" reference actually refers to the DisconnectAction instance ! Does it make any difference what class instance is used ? In other words, why is the getResource method not static ? Has it something to do with class loaders (not my specialit´┐Ż..) ?

Regards, Klaas
 
mike acre
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Klaas van Gelder:
This code appears to work, i found an example in a newsgroup.
Running the executable Jarfile goes OK, when i start the client using the non-packaged version, i need to add the current dir to the classpath:
java -cp .;classes suncertify.client.swingview.RunClient



I do not completely understand the code. The "this" reference actually refers to the DisconnectAction instance ! Does it make any difference what class instance is used ? In other words, why is the getResource method not static ? Has it something to do with class loaders (not my specialit´┐Ż..) ?

Regards, Klaas


No, it doesn't it is just to get a handle on the class loader.

Always seem to do that getResource business inside an anonymous class, I usually use

MyTopLevelClass.class.getResource() just for clarity.

I think if you use this.getClass() it is slightly less clear your intent.

The procedure you have stumbled on is most excellent, check out the brother getResourceAsStream() equally useful.

I've used these to load default property resources, icons & help files.
 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx For the feedback ! The class ClassLoader btw defines some comparable methods, at the moment I use:

URL url = ClassLoader.getSystemResource("images/Host24.gif");

So a static method can be used. I am still investigating the differences between these possibilities. I wonder whether Sun will expect you to document such very detailed decisions... :-)
Grtz Klaas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic