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

javaspringspring-boot

I am developing a Spring Boot application which has a dependency added. This dependency has a spring.xml file. I am scanning this xml file and creating beans as well. One of the beans is looking for hibernate.properties in classpath. I have added this property file under resources folder of my application. However I still see the exception listed below. Please can you let me know what I am missing?

 <util:properties id="HibernateProperties" location="classpath:hibernate.properties"/>

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

Best Answer

Here some doc from spring

Ant-style patterns with classpath: resources are not guaranteed to find matching resources if the root package to search is available in multiple class path locations. This is because a resource such as

com/mycompany/package1/service-context.xml may be in only one location, but when a path such as

classpath:com/mycompany/**/service-context.xml

so,

<util:properties id="HibernateProperties" location="classpath*:hibernate.properties"/> 

may give you better chance.

but,

Please note that classpath*: when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like classpath*:*.xml will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK’s ClassLoader.getResources() method which only returns file system locations for a passed-in empty string (indicating potential roots to search).

Related Topic