Java - class.getResource 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4301329/
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-08-14 15:37:12  来源:igfitidea点击:

Java - class.getResource returns null

javaurl

提问by Buddhi

I am using the following to get the URL of this particular file, but it returns null. Does anyone have any suggestions as to the problem or an alternate way to do this?

我正在使用以下内容来获取此特定文件的 URL,但它返回 null。有没有人对这个问题或替代方法有任何建议?

URL url = ExchangeInterceptor.class.getResource("GeoIP.dat");

回答by javamonkey79

No, that is the right way afaik. Make sure the resource is on your classpath. This is often the cause of these types of problems.

不,这是正确的方式afaik。确保资源在您的类路径上。这通常是导致这些类型问题的原因。

回答by Martin Algesten

The path is relative to the classpath root and if you don't give an absolute path, it is looking in the same package as the class you're using (in this case ExchangeInterceptor). To find something in the root use /GeoIP.dat.

该路径是相对于类路径根目录的,如果您不提供绝对路径,它会在与您使用的类相同的包中查找(在本例中为ExchangeInterceptor)。要在 root 中找到一些东西,请使用/GeoIP.dat.

回答by iirekm

Where do you have put this GeoIP.dat? In the same package as ExchangeInterceptor, or in the "root" package. If in the same package, your code is OK, if in the root - add '/' prefix.

你把这个 GeoIP.dat 放在哪里?在与 ExchangeInterceptor 相同的包中,或在“root”包中。如果在同一个包中,则您的代码没问题,如果在根目录中 - 添加“/”前缀。

Maybe you're using M2Eclipse? If configured incorrectly, it also may result in such problems. Another cause of such problems may be: misconfigured classloaders, misconfigured OSGi, ...

也许您正在使用 M2Eclipse?如果配置不正确,也可能导致此类问题。此类问题的另一个原因可能是:错误配置的类加载器、错误配置的 OSGi、...

回答by merovin

Just in case someone still has problems to understand that:

以防万一有人仍然有问题要理解:

.getResource() grants you access to the local bin folder. That means, your resources need to be located in YourProject/bin/package/. The root folder is YourProject/bin/ and can be accssed by adding the prefix / to the String argument, like iirekm said.

.getResource() 授予您访问本地 bin 文件夹的权限。这意味着,您的资源需要位于 YourProject/bin/package/ 中。根文件夹是 YourProject/bin/ 并且可以通过将前缀 / 添加到 String 参数来访问,就像 iirekm 说的那样。

回答by Jakub Zaverka

First, you need to make sure you are accessing the right file on the right path. You can verify that by getClass().getResource("GeoIP.dat").getAbsolutePath().

首先,您需要确保在正确的路径上访问正确的文件。您可以通过 getClass().getResource("GeoIP.dat").getAbsolutePath() 来验证。

Secondly, the path specifier is case-sensitive, so make sure your file is not named "geoIP.dat" or "GeoIP.DAT".

其次,路径说明符区分大小写,因此请确保您的文件未命名为“geoIP.dat”或“GeoIP.DAT”。

回答by yggdraa

For those who use Intellij Idea: check for Settings -> Compiler -> Resource patterns.

对于那些使用Intellij Idea 的人:检查Settings -> Compiler -> Resource patterns

The setting contains all extensions that should be interpreted as resources. If an extension does not comply to any pattern here, class.getResource will return null for resources using this extension.

该设置包含应解释为资源的所有扩展。如果扩展不符合此处的任何模式,则 class.getResource 将为使用此扩展的资源返回 null。

回答by Peter Jamieson

The file needs to be in the classpath, e.g.: -

该文件需要在类路径中,例如:-

bin/my/package/GeoIP.dat

bin/my/package/GeoIP.dat

The / prefix seems to be a lie. The following would work.

/ 前缀似乎是一个谎言。以下将起作用。

URL url = ExchangeInterceptor.class.getResource("my/package/GeoIP.dat");

I suspect the issue is that you do not have the file in the classpath.

我怀疑问题是您在类路径中没有该文件。

回答by Wins

Use the getResourcemethod of the class' ClassLoader

使用getResource类的方法'ClassLoader

URL url = ExchangeInterceptor.class.getClassLoader().getResource("GeoIP.dat");

回答by Semmel

In case of eclipse.

日食的情况下。

Just a hint. Your code could be correct, but your jre configuration not. I ran into the the same error, nothing helped, until i checked the eclipse settings.

只是一个提示。您的代码可能正确,但您的 jre 配置不正确。我遇到了同样的错误,没有任何帮助,直到我检查了 eclipse 设置。

Make sure, that you set your execution environment right.

确保您设置了正确的执行环境。

Preferences -> Java -> Installed JREs -> use "jdk..." as compatible JRE 

回答by MewX

I solved this problem by pointing out the resource rooton IDEA.

我通过resource root在 IDEA 上指出来解决了这个问题。

Initially the directory was so and the icon was a plain folder icon

最初目录是这样,图标是一个普通的文件夹图标

Before

enter image description here

在此处输入图片说明

Right clickon a directory (or just the project name) -> Mark directory As-> Resource Root.

Right click在目录(或只是项目名称)上 -> Mark directory As-> Resource Root

After

after

后

Recompile & rejoice :P

重新编译并高兴:P