Spring Boot project can't find Hibernate.cfg.xml when run as executable jar

Hi all, I’m new to both these forums and Hibernate in general. I’ve got a little project working in Eclipse but not when I try and run it from an executable jar file (this is in a Java Spring Boot environment) it complains that it can’t locate the hibernate.cfg.xml file. I see that issues with the hibernate.cfg.xml not being found when SessionFactory is being set up is a fairly common occurrence, but my question here is somewhat specific and I’m hoping someone out there can help me out. I put the hibernate.cfg.xml file into main/resources and do this:

    Configuration configuration = new Configuration();
    configuration = new Configuration();
    configuration.configure();

Which works in Eclipse. However, when I build an executable jar to run the application elsewhere, when it launches it complains:

Initial SessionFactory creation failed.org.hibernate.internal.util.config.ConfigurationException: Could not
locate cfg.xml resource [hibernate.cfg.xml] org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

I understand (or I think I do) that the reason for this is that the hibernate.cfg.xml isn’t getting included in the jar itself and so even when the path is defined it can’t find it but am not sure how to fix it. I think if I can get the file into the jar then it may work (maybe the classpath also needs an entry for this?) but I’m actually not sure how to do so. I feel like this probably gets done a lot and I’m missing some simple thing…can anyone help me out?

Use Maven of Gradle to build a project, not the IDE which is to ease development.

There are plenty of Maven plugins and project templates to use which will include everything located in the resources folder in the jar.

Hi Vlad, thanks for the response - I tried installing the Maven plugin for Eclipse and building with that, but it fails (it looks like it’s because this is a Spring Boot project and not a Maven project…it fails to find the pom.xml file. Eclipse has an option to convert it to a Maven project, do you recommend trying that?

I don’t use Eclipse but IntelliJ. However, there are plenty of examples using Maven and Gradle in Spring guides.