Class.getResource() 在我的 Eclipse 应用程序中返回 null?无法配置类路径?

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

Class.getResource() returns null in my Eclipse Application? Can't configure classpath?

javaeclipseclasspath

提问by Buttons840

I am trying to use Class.getResource("rsc/my_resource_file.txt")to load a file in an Eclipse application. However, no matter what I do in Eclipse the classpath always contains just one entry to the Eclipse Launcher:

我正在尝试用于Class.getResource("rsc/my_resource_file.txt")在 Eclipse 应用程序中加载文件。但是,无论我在 Eclipse 中做什么,类路径始终只包含一个到 Eclipse Launcher 的条目:

.../eclipse/plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.pkc

.../eclipse/plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.pkc

How can I configure the classpath?

如何配置类路径?

Note: At runtime I am determining the classpath with the following code:

注意:在运行时,我使用以下代码确定类路径:

URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
for (URL classpathURL : cl.getURLs()) {
    System.out.println(classpathURL);
}

EDIT: Further information.

编辑:更多信息。

The root of the problem is that Class.getResource("rsc/my_resource_file.txt")is returning null. Having done some small experiments in a simple 5 line "Java Application" I thought I had figured it out and that the problem was related to the classpath. Apparently the classpath behaves a little different with an "Eclipse Application". I solved the problem by doing Class.getResource("/rsc/my_resource_file.txt")Thanks BalusC.

问题的根源Class.getResource("rsc/my_resource_file.txt")在于返回空值。在一个简单的 5 行“Java 应用程序”中做了一些小实验后,我以为我已经弄清楚了,问题与类路径有关。显然,类路径的行为与“Eclipse 应用程序”略有不同。我通过执行Class.getResource("/rsc/my_resource_file.txt")Thanks BalusC解决了这个问题。

回答by BalusC

Please take a step back. Your concrete problem is that the resource returns null, right? Are you sure that its path is right? As you have, it's relative to the package of the current class. Shouldn't the path start with /to be relative to the package root?

请退后一步。您的具体问题是资源返回null,对吗?你确定它的路径是正确的吗?正如您所拥有的,它与当前类的包有关。路径不应该/以相对于包根目录开始吗?

URL resource = getClass().getResource("/rsc/my_resource_file.txt");
// ...

Alternatively, you can also use the context class loader, it's always relative to the classpath (package) root:

或者,您也可以使用上下文类加载器,它始终相对于类路径(包)根:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL resource = loader.getResource("rsc/my_resource_file.txt");
// ...

At least, the Eclipse launcher is not to blame here.

至少,Eclipse 启动器不应归咎于此。

回答by duffymo

Right click on the project and follow the properties.

右键单击项目并按照属性进行操作。

回答by DwB

Put the file in the top level directory in your source tree. This is often called "src". Then, when you build your project the file will be copied into your class directory (name varies). Finally, post build the file will be in your classpath (within the eclipse environment).

将文件放在源代码树的顶级目录中。这通常称为“src”。然后,当您构建项目时,该文件将被复制到您的类目录中(名称各不相同)。最后,构建后文件将位于您的类路径中(在 eclipse 环境中)。

Class someClassObject = BlammyClassName.class;
someClassObject.getResource("my_resource_file.txt");

will return a URL to your resource.

将返回一个 URL 到您的资源。

someClassObject.getResourceAsStream("my_resource_file.txt");

will return a stream.

将返回一个流。

Edit: changed such that it does not reference Class methods statically.

编辑:更改为不静态引用类方法。

回答by keesp

I've had this problem occur, because the The Bundle-ClassPath setting in Manifest.MF MUST include . (see https://www.eclipse.org/forums/index.php/t/287184/), for instance:

我遇到过这个问题,因为 Manifest.MF 中的 Bundle-ClassPath 设置必须包含 . (参见https://www.eclipse.org/forums/index.php/t/287184/),例如:

Bundle-ClassPath: .,
 test.myapp.core

Bundle-ClassPath is not added automatically by the plugin wizards, so that can cause some problems.

Bundle-ClassPath 不是由插件向导自动添加的,因此可能会导致一些问题。