12

I'm trying to do an initial setup with Hibernate in Eclipse, while deploying with Tomcat.

I encountered the following problem:

java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    db.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    db.HibernateUtil.<clinit>(HibernateUtil.java:8)

I've goggled my problem online and I understand that it has something to to with the project class path.

I've added a user library to my project containing all the jars under the "required" directory in the hibernate download, but that doesn't seem to work.

Please find a screen shot of my project here:

2
  • have you tried putting your hibernate jars directly in the build path? Commented Mar 24, 2012 at 11:58
  • As per your attached image, I think your DOCTYPE entry in your hibernate.cfg.xml file. Replace with below entry and test it.. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> Commented Mar 24, 2012 at 14:41

5 Answers 5

24

You're getting the error because the hibernate libraries are not available to Tomcat. In your picture, below the hibernate library set there is an empty set called 'Web App Libraries' - this is the set your hibernate libraries need to be in.

Right click your project -> Build Path -> Configure Build Path, and remove the hibernate set from the build path. Now import the jars into the WEB-INF/lib folder. Refresh your project and now you should see them listed in the 'Web App Libraries' set (i appreciate this is somewhat annoying that you have to import them into your code base - someone else might know a better way to do this that doesn't involve copying the jars in)

Sign up to request clarification or add additional context in comments.

3 Comments

After doing this where should you place the hibernate configuration file?
@Chris White how about in IntelliJ IDEA?
@aircraft late but see my answer below
20

This error can happen when your jar files has not been exported with your web archive. You can do what @Chris White has said or you can follow below steps if you are using Eclipse.

Right click the project -> Build Path -> Configure Build Path

In the list at left side click on Deployment Assembly. Click "Add" button. Select "Java Build Path Entries" and click next. Now select your hibernate jar files or library. Click finish. Then click Ok.
Restart the server if necessary.

2 Comments

This solution is more elegant as it does not requires to copy the library in the project. I find it's better to reuse dependencies from the container instead of copying them over all the time.
Faced a similar problem and thanks to your answer was able to solve it!!!
3

Simpler way than Chris White's way.

You're getting the error because the hibernate libraries are not available to Tomcat. Right click your project -> Build Path -> Configure Build Path, and remove the hibernate set from the build path.

Do the same stuff, but instead of importing the jars everytime for a new project, **copy the required hibernate jars in lib folder of Apache Tomcat **(or whatever the server u are using).

Comments

2

You don't have the hibernate jar present in your library folder Visit the below link for the possible solution, http://www.smashplus.info/2012/11/javalangnoclassdeffounderror.html

Comments

0

To achieve the same with IntelliJ :

  • File -> Project Structure
  • Project Settings -> Artifacts
  • Select your .war artifact
  • expand Available Elements
  • right click the Hibernate lib -> Put into /WEB-INF/lib

Done.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.