Problem loading resources while running in Eclipse

岁酱吖の 提交于 2019-11-30 22:14:16

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.

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

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.

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

Can you try loading the resources using below?

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

This might be useful information

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

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.

  1. Enable Dependency Management.
  2. Update Project Configuration
  3. (In Eclipse) Project -> Build Project
  4. (In Eclipse) Run Application

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!