简体   繁体   中英

java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

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:

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)

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.

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).

您的库文件夹中没有hibernate jar请访问以下链接以获取可能的解决方案, http://www.smashplus.info/2012/11/javalangnoclassdeffounderror.html

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM