带有 Eclipse 的 Java 类 getResource()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12341348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:29:43  来源:igfitidea点击:

Java class getResource() with eclipse

javagetresource

提问by Nicolas Martel

i've been trying to get getResource to work correctly this whole morning but so far the only way i've had to get it working is by moving the res folder to the bin folder and then using getResource like so

我整个上午都在试图让 getResource 正常工作,但到目前为止,我必须让它工作的唯一方法是将 res 文件夹移动到 bin 文件夹,然后像这样使用 getResource

URL url = MyClass.class.getResource("/res/gfx/entity/entity1.png");

But is there not any way i can make it so res is outside the bin folder?

但是有没有什么办法可以让 res 在 bin 文件夹之外?

回答by Nicolas Martel

Nevermind, i can't believe how stupid of a fix that was.

没关系,我无法相信这是多么愚蠢的修复。

I added the res folder to the main project folder rather than bin, then went in eclipse

我将 res 文件夹添加到主项目文件夹而不是 bin,然后进入 eclipse

run > run configurations > classpath > select user entries > advanced > add folders > select res

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

then when calling a folder from res, rather than doing "/res/gfx/entity/entity1.png", i did "/gfx/entity/entity1.png"

然后当从 res 调用文件夹时,而不是执行“/res/gfx/entity/entity1.png”,我做了“/gfx/entity/entity1.png”

Problem solved!

问题解决了!

回答by Zagrev

Normally, in a Java project, you would add the /res folder as a source folder in the project settings.

通常,在 Java 项目中,您会在项目设置中添加 /res 文件夹作为源文件夹。

In a Plugin project, you would add the folder to the output in the build.properties.

在插件项目中,您可以将该文件夹添加到 build.properties 的输出中。

In a Dynamic Web project, you would put the /res folder in the WebContent folder.

在动态 Web 项目中,您会将 /res 文件夹放在 WebContent 文件夹中。