Can I use URL when executing java code in eclipse

2019-09-13 21:43发布

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?

0条回答
登录 后发表回答