如何将直接访问的资源打包到jar文件中 [英] How to package resources, that are accessed directly , into a jar file

查看:218
本文介绍了如何将直接访问的资源打包到jar文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Slick2D开发了一款游戏,我已经直接访问了我的所有图像,例如

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.

我想知道是否仍然可以将所有资源加载到jar中,我将 / res 文件夹添加到我的构建路径中使用jarsplice来添加我的库和本机,但是jar无法运行,因为它无法找到图像。

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.

推荐答案

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

正在查看运行应用程序的根文件系统。如果你想使用jar文件中包含的资源,你必须这样做:

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

编辑

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

这篇关于如何将直接访问的资源打包到jar文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆