我的资源文件夹应该与可运行的 jar 一起放在哪里? [英] Where should my resource folder be with a runnable jar?

查看:49
本文介绍了我的资源文件夹应该与可运行的 jar 一起放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Eclipse 中,我有一个项目正在处理资源,例如我称之为这样的资源

In eclipse, i have a project i am working on with resources that i call like this for example

URL url = FighterComponent.class.getResource("/gfx/characters/");
File file = new File(url.getPath() + info.name + "/" + frame.path);

/gfx/是 res 中的一个文件夹.res 是我通过这样做链接的资源文件夹:

/gfx/ being a folder inside of res. res is my resource folder which i have linked by doing this:

运行 > 运行配置... > 类路径 > 选择用户条目 > 高级 > 添加文件夹

run > run configurations... > Classpath > Select user entries > Advanced > Add folder

我现在的问题是,在我将项目导出到可运行的 jar 以便我的朋友能够测试之后,res 文件夹应该在哪里?我试着把它和我的 jar 放在同一个文件夹中,在我的 jar 里面,并且 URL 都是空的.

My question now is that where should the res folder be after i exported my project into a runnable jar for my friends to be able to test it? I tried putting it in the same folder as my jar and inside my jar and for both the URL was null.

有什么想法吗?

推荐答案

如果名称以 '/' ('\u002f') 开头,则绝对资源的名称是 '/' 后面的名称部分.

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

因此,您应该在 jar 文件中有一个 gfx/characters 目录.在此目录下,如果您有 image.jpg 资源,则必须使用以下方法检索它:

So, you should have a gfx/characters directory inside the jar file. Under this directory, if you have an image.jpg resource, you will have to retrieve it using:

URL url = TheClass.class.getResource("/gfx/characters/image.jpg");

您还可以使用 getResourceAsStream() 来获取您可以从中读取的 InputStream.

You can also use the getResourceAsStream() to get an InputStream that you can read from.

这篇关于我的资源文件夹应该与可运行的 jar 一起放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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