java.lang.ClassNotFoundException: org.hibernate.HibernateException

I am running a web application with hibernate and got stuck at this exception. Any help please?

java.lang.ClassNotFoundException: org.hibernate.HibernateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at com.forum.dao.TopicDAO.findAll(TopicDAO.java:43) at com.forum.servlets.Accueil.doGet(Accueil.java:23) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

Here is TopicDAO.findALL()

public static List<Topic> findAll() {

    Session  s = HibernateUtils.getSession(); //TopicDAO.java:43
    Transaction tx = s.beginTransaction();
    List<Topic> objects = null;

    Query q = s.createQuery("from Topic");
    objects = q.list();
    tx.commit();

    return objects;
}

And, here is my servlet.doGet()

public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
    HttpSession session = request.getSession();

    TopicDAO td = new TopicDAO();
    List<Topic> listTopics = td.findAll();

    session.setAttribute( ATT_LIST_TOPICS, listTopics );

    this.getServletContext().getRequestDispatcher( ACCUEIL ).forward( request, response );
}

Here is my added Hibernate libraries:

hibernate structure


Solution 1:

Explanation:

  • A ClassDefNotFound exception means that your program was unable to find a required .class file from the referenced libraries.

  • In your case the hibernateX.jar file isn't packaged inside your war file.

  • What you need to do is to add it to the WEB-INF/lib folder of your war file.

Solution (using IntelliJ):

  1. open up project structure

  2. select Artifacts from the left side options

  3. from your war file in the OutputLayout tab browse to WEB-INF/lib

  4. add the library containing hibernateX.jar into the folder

  5. redeploy your project.

Solution (using Eclipse):

  1. Simply drag and drop the jar to WEB-INF/lib

Solution 2:

I see a few of the libraries are missing. I have these and it works just fine. Btw. what database do you use? PostgreSQL? Otherwise you need to also include the JDBC driver for your database. Please note that some of these are required for hibernate use through JPA.

enter image description here