2

Here I am creating a simple Registration form using JSP and Hibernate in Eclipse Mars. And as I run a jsp page I am getting an exception:

HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/hibernate/Session.

Here I put required files and directory structure.

1)User.java

package com.nick.mypack;
 public class User {

 private int id;
 private String name,password,email;

 public int getId() {
    return id;
 }
 public void setId(int id) {
    this.id = id;
 }
 public String getName() {
    return name;
 }
 public void setName(String name) {
    this.name = name;
 }
 public String getPassword() {
    return password;
 }
 public void setPassword(String password) {
    this.password = password;
 }
 public String getEmail() {
    return email;
 }
 public void setEmail(String email) {
    this.email = email;
 }

 }

2)UserDao.java

package com.nick.mypack;

import org.hibernate.Session;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  

public class UserDao {  

 public static int register(User u){  
 int i=0;  
 Session session=new Configuration().  
 configure().buildSessionFactory().openSession();  

 Transaction t=session.beginTransaction();  
 t.begin();  

 i=(Integer)session.save(u);  

 t.commit();  
 session.close();  

return i;  
}  
}  

3)index.jsp

<html>
  <form action="register.jsp" method="post">  
  Name:<input type="text" name="name"/><br><br/>  
  Password:<input type="password" name="password"/><br><br/>  
  Email ID:<input type="text" name="email"/><br><br/>  
  <input type="submit" value="register"/>"  

  </form>
</html>

4)register.jsp

<%@page import="com.nick.mypack.UserDao" %>
 <jsp:useBean id="obj" class="com.nick.mypack.User">  
 </jsp:useBean>  
 <jsp:setProperty property="*" name="obj"/>  

<%   
 int i=UserDao.register(obj);  
 if(i>0)  
 out.print("You are successfully registered");  
%>    

This is a directory structure of a Project.

Please help me to solve this. Thanks in advance!

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Nisarg
  • 63
  • 1
  • 3
  • 8

2 Answers2

1

Well, your error is:

java.lang.NoClassDefFoundError: org/hibernate/Session.

This means that your container (Tomcat or whatever server you are using) cannot load the class. You are probably not packaging your deployment right.

You need the Hibernate jars in WEB-INF/lib. It looks empty here, so they are not on the classpath when your app is running.

mikeb
  • 10,578
  • 7
  • 62
  • 120
  • I tried to copy jars into WEB-INF/lib but error message "you cant copy those files" shows up. And Servlet class files not getting generated into build/classes folder so sometimes it shows 404 error, too. – Nisarg Feb 01 '16 at 05:29
  • That's an entirely different problem, and a different question. – mikeb Feb 01 '16 at 13:17
  • Well I copied jars into lib and its working fine! Thank you! – Nisarg Feb 04 '16 at 05:15
0

Make sure that the configuration is correct

Configuration

  1. Download Hibernate 5 from [here][1].
  2. Extract the Required jars from the folder inside and add it to the build path in following hibernate-release-5.0.7.Final.zip\hibernate-release-5.0.7.Final\lib\required.
  3. Make sure the following jars are added to the classpath (this should be in your lib folder in WebApp) of the project:

    antlr-2.7.7.jar
    commons-dbcp2-2.1.1.jar
    dom4j-1.6.1.jar
    geronimo-jta_1.1_spec-1.1.1.jar
    hibernate-commons-annotations-5.0.1.Final.jar 
    hibernate-core-5.0.7.Final.jar
    hibernate-jpa-2.1-api-1.0.0.Final.jar
    jandex-2.0.0.Final.jar
    javassist-3.18.1-GA.jar
    javax.servlet.jsp.jstl-api-1.2.1-sources.jar
    jboss-logging-3.3.0.Final.jar
    sqljdbc4-3.0.jar //or whatever database you use
    hibernate-entitymanager-5.0.7.Final
    
  4. HibernateUtil.java :

    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
    
         private static final SessionFactory sessionFactory = buildSessionFactory();
    
         private static SessionFactory buildSessionFactory() {
         try {
            // Create the SessionFactory from hibernate.cfg.xml
              return new Configuration().configure().buildSessionFactory();
         }
         catch (Throwable ex) {
           // Make sure you log the exception, as it might be swallowed
          System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
          }
        }
    
       public static SessionFactory getSessionFactory() {
          return sessionFactory;
       }
    
       public static void shutdown() {
        // Close caches and connection pools
         getSessionFactory().close();
       }
    
     }
    
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108