How to package resources, that are accessed directly , into a jar file

淺唱寂寞╮ 提交于 2019-12-30 10:32:36

问题


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


来源:https://stackoverflow.com/questions/12713861/how-to-package-resources-that-are-accessed-directly-into-a-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!