在 Eclipse 中运行时加载资源时出现问题

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

Problem loading resources while running in Eclipse

javaeclipsemaven-2classloader

提问by Alexandre

I'm working on a swing project, using maven2 (from command-line) and eclipse (without maven integration). So, I generate the eclipse project through maven eclipse plugin(mvn eclipse:eclipse), import it inside eclipse, and do all my work.

我正在使用 maven2(来自命令行)和 eclipse(没有 maven 集成)进行一个 Swing 项目。所以,我通过maven eclipse plugin( mvn eclipse:eclipse)生成eclipse项目,导入eclipse里面,做我所有的工作。

My problem is: when I run my app in eclipse (as a Java Application), I can't find none of the resources that are in my src directory. Digging for information on my problem, I get into this answer from another question. So, I compared the output from the following instructions:

我的问题是:当我在 eclipse 中运行我的应用程序(作为 Java 应用程序)时,我在 src 目录中找不到任何资源。挖掘有关我的问题的信息,我从另一个问题中得到了这个答案。因此,我比较了以下说明的输出:

MyClass.class.getResource("/").getPath();
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toString();

Those gave me the following outputs, respectively:

这些分别给了我以下输出:

${workspace_loc}/${my_project}/target/test-classes/ file:/${workspace_loc}/${my_project}/target/classes/

${workspace_loc}/${my_project}/target/test-classes/ 文件:/${workspace_loc}/${my_project}/target/classes/

Checking the above locations, I could see that the former is empty, while the other one contained all my compiled classes and resources. So, I came to the conclusion that the classloader is looking for my resources in the wrong place. So, I think I have three questions:

检查上面的位置,我可以看到前者是空的,而另一个包含我所有编译的类和资源。所以,我得出结论,类加载器在错误的地方寻找我的资源。所以,我想我有三个问题:

  1. Is my understanding correct?
  2. If so, how it does to find the classes it is loading?
  3. How do I solve this?
  1. 我的理解正确吗?
  2. 如果是这样,它如何找到它正在加载的类?
  3. 我该如何解决这个问题?

UPDATE: I've changed my code, so instead of invoking MyClass.class.getResource(...)or MyClass.class.getResourceAsStream(...), I'm now using ClassLoader.getSystemResource(...)and ClassLoader.getSystemResourceAsStream(...). In this way, everything is working fine in eclipse. I just don't know exactly why. Any hint on this?

更新:我已经更改了我的代码,因此我现在使用的是and ,而不是调用MyClass.class.getResource(...)or 。这样,在 eclipse 中一切正常。我只是不知道确切的原因。这有什么提示吗?MyClass.class.getResourceAsStream(...)ClassLoader.getSystemResource(...)ClassLoader.getSystemResourceAsStream(...)

回答by Nicolas Bousquet

Two possibles cases for me :

对我来说两种可能的情况:

  • You are using eclipse to compile your project. Then eclipse is configured to exclude (or not include) resources in the src folder. You can set it in Project/Properties/Java Build Path/Source. Then you expend your src folder, and ensure you have something like "Included All", "Excluded None".

  • You run your maven application using maven to compile and not eclipse, even though you are using eclipse as your IDE. Then by default maven will not copy resources from the source folder to the output folder... Because it is not the standard maven way of doing things. Thus the resource are missing from the classpath and you don't find them. Just change your maven configuration to also include resources from your source folder.

  • 您正在使用 eclipse 来编译您的项目。然后将 eclipse 配置为排除(或不包括)src 文件夹中的资源。您可以在 Project/Properties/Java Build Path/Source 中进行设置。然后你扩展你的 src 文件夹,并确保你有“包含所有”、“不排除无”之类的东西。

  • 即使您使用 eclipse 作为您的 IDE,您也可以使用 maven 编译而不是 eclipse 来运行您的 maven 应用程序。那么默认情况下maven不会将资源从源文件夹复制到输出文件夹......因为这不是标准的maven做事方式。因此,类路径中缺少资源,而您找不到它们。只需更改您的 Maven 配置以包含源文件夹中的资源。

回答by Eugene Ryzhikov

I think you have to add src/main/resourcesto the build path. This is done in Project Properties > Build Path > Source. Here is how the standard maven project looks:

我认为您必须添加src/main/resources到构建路径中。这是在Project Properties > Build Path > Source. 以下是标准 Maven 项目的外观:

enter image description here

在此处输入图片说明

In future when using Eclipse I suggest to use m2eclipse plugin and create project using it. This will automatically make sure that all these folders are in the build path.

将来使用 Eclipse 时,我建议使用 m2eclipse 插件并使用它创建项目。这将自动确保所有这些文件夹都在构建路径中。

回答by jilt3d

Try this: Run configurations... -> Classpath -> User Entries -> Advanced... -> Add Folders

试试这个:运行配置... -> 类路径 -> 用户条目 -> 高级... -> 添加文件夹

回答by Prabhjot

Can you try loading the resources using below?

您可以尝试使用下面的方法加载资源吗?

this.getClass().getClassLoader().getResourceAsStream(propertyFileName)

回答by Hardik Mishra

This might be useful information

这可能是有用的信息

Eclipse Maven plugin has its own Classpath Container that conflicts with generated class paths when enabled.

Eclipse Maven 插件有自己的类路径容器,当启用时会与生成的类路径冲突。

回答by John Vint

What I would suggest is stop using eclipse:eclipse (sorry - harsh I know). I used it for about 8 months, cant believe it took me that long, and used M2Eclipse. M2Eclipse is an eclipse plugin in which you do the following.

我的建议是停止使用 eclipse:eclipse (对不起 - 我知道很苛刻)。我用了大约 8 个月,不敢相信我花了这么长时间,并使用了 M2Eclipse。M2Eclipse 是一个 Eclipse 插件,您可以在其中执行以下操作。

  1. Enable Dependency Management.
  2. Update Project Configuration
  3. (In Eclipse) Project -> Build Project
  4. (In Eclipse) Run Application
  1. 启用依赖管理。
  2. 更新项目配置
  3. (在 Eclipse 中)项目 -> 构建项目
  4. (在 Eclipse 中)运行应用程序

It may not seem it, but its a much easier and less frustrating way of doing it.

看起来可能不是这样,但它是一种更容易且不那么令人沮丧的方法。

Before, with eclipse:eclipse. You would have to run it then hit refresh and hope that everything is configured - if you have a multi-module pom things can really go wrong.

之前,用 eclipse:eclipse。你必须运行它然后点击刷新并希望一切都配置好 - 如果你有一个多模块 pom 事情真的会出错。

回答by Vijay Anantharamu

For me the Files within the WEB-INF were not getting included. Hence I added them to Buildpath (Project -> Build Path -> Configure Build Path -> Add folder (project/src/main/webapp). This resolved the issue.

对我来说,WEB-INF 中的文件没有被包含在内。因此我将它们添加到 Buildpath (Project -> Build Path -> Configure Build Path -> Add folder (project/src/main/webapp)。这解决了问题。