Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - hibernate.cfg.xml File Not Found

I have my hibernate.cfg.xml located at src/main/resources/ but I keep getting this error when I run my maven project, I have also tried putting the path to the file in the configuration like this but still got the error

Configuration conf = new Configuration();  
conf.configure("/src/main/resources/hibernate.cfg.xml"); 

What am I doing wrong?

And when I go to the properties of the project and go to Source I see /src/main/resources in the build path?

It also runs when I make it in eclipse, but when I export to jar it stops working but I have it in class path as you can seeenter image description here

Thanks

Edit

Here is my directory in my eclipse project

enter image description here

And then here is it when I open the .jar file

enter image description here

Are you saying the .xml should be in the root directory?

like image 569
spen123 Avatar asked Jul 30 '15 18:07

spen123


People also ask

How do I fix Hibernate cfg XML not found?

The hibernate. cfg. xml file shoul be in root directory of the classpath of your project. If you using Maven then make sure it should be like src > resources > hibernate.

How add Hibernate cfg XML in Intellij?

Right click on the desired module -> Add -> Hibernate. Select the newly created Hibernate configuration option, and click the (+) sign in the right pane to create hibernate. cfg. xml file.

Where Hibernate cfg XML location?

cfg. xml to specify required Hibernate properties in my examples. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application's classpath.


1 Answers

There is no /src/main/resources in your built project, that is just where maven keeps the files before building. While building the project (I assume you are building a .jar-artifact) maven will do the following:

  1. All .java-files in src/main/java will be compiled to .class-files and will be moved to the subdirectory of the jar corresponding to their package
  2. All files from src/main/resources will be copied to the root of the jar-file

So this file-structure in your project:

src
|
+-main
   |
   +-java
   |   |
   |   +- mypackage
   |          |
   |          +- MyClass.java
   |
   +-resources
         |
         +- hibernate.cfg.xml

will end up in your .jar-file like this:

project.jar
|
+- mypackage
|     |
|     +- MyClass.class
|
+- hibernate.cfg.xml

The essential point is: All files from /src/main/resources will end up in the classpath of your built project. But since your configuration-file is named hibernate.cfg.xml and is located on the classpath you should be OK just doing

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

Note that configure() is called wthout an argument, so that means "Look for a file called hibernate.cfg.xml on your current classpath". So try removing the argument from your configure-call.

like image 191
piet.t Avatar answered Oct 02 '22 15:10

piet.t