0

My simple safari simulator uses some png images to display output. I originally loaded the images using:

lionImage = Toolkit.getDefaultToolkit().getImage("images/lion.png");

This worked like a charm until I tried to export my program as a jar file. A quick search found the solution, so now I use:

lionImage = Toolkit.getDefaultToolkit().getImage(
                GameReserve.class.getResource("images/lion.png"));

...which works fine in the jar but won't run in eclipse!

So my question is do I really need two different versions of my code (one to run when I am developing the project and another for export to jar)? Or can they be combined to work in both environments?

pajaja
  • 2,164
  • 4
  • 25
  • 33
BendyMan
  • 301
  • 1
  • 3
  • 8
  • See http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – vikingsteve Feb 13 '15 at 11:43
  • It is explained very nicely in the link provided.. You can use `GameReserve.class.getClassLoader().getResource("image/lion.png")` instead of what you are using now, which will search wrt contextPath everytime.. – karthik manchala Feb 13 '15 at 12:04
  • Thanks guys. A useful link, especially this link in that answer... http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html – BendyMan Feb 14 '15 at 00:34

0 Answers0