java.io.FileNotFoundException: class path resource cannot be opened because it does not exist

别说谁变了你拦得住时间么 提交于 2019-11-29 00:52:41

问题


I am trying to set the configuration location for my Project but I keep getting the following error:

java.io.FileNotFoundException: class path resource [main/resources/app-context.xml] cannot be opened because it does not exist

I have my project set up like this:

And I have my code set up as:

ApplicationContext context = new ClassPathXmlApplicationContext(configLocation: "main/resources/app-context.xml");

How can I fix this?


回答1:


What you put directly under src/main/java is in the default package, at the root of the classpath. It's the same for resources put under src/main/resources: they end up at the root of the classpath.

So the path of the resource is app-context.xml, not main/resources/app-context.xml.




回答2:


We can also try this solution

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:app-context.xml");

in this the spring automatically finds the class in the class path itself




回答3:


Try this:

ApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");



回答4:


The file location/path has to relative to your classpath locations. If resources directory is in your classpath you just need "app-context.xml" as file location.



来源:https://stackoverflow.com/questions/42863382/java-io-filenotfoundexception-class-path-resource-cannot-be-opened-because-it-d

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