How to package resources, that are accessed direct

2019-02-18 22:23发布

问题:

I have recently developed a game in Slick2D, i have accessed all my images directly e.g

Image i = new Image("address.png");

as opposed to using a class that will load resources or using an input stream.

I wondered if it would still be possible to load all the resources into a jar, i added the /res folder to my buildpath and used jarsplice to add my libraries and natives however the jar will not run because it cannot find the images.

回答1:

Image i = new Image("address.png");

Is looking into the root filesystem where your application is running. If you want to use the resources packed in your jarfile you must do:

Image i = new Image(getClass().getResource("/res/address.png").toURI()); // In case your Image object accepts URI as parameters

EDIT

Image i = new Image(getClass().getResource("/res/address.png").toExternalForm()); // Since your object only accept Strings