Link to home
Start Free TrialLog in
Avatar of muthiahmerchant
muthiahmerchant

asked on

Hibernate java.lang.NoClassDefFoundError

Hi I am trying a simple Hibernate example. But I am getting a NoClassDefFoundError. initially i did not have dom4j etc in my /lib folder but at that time it used to indicate which class is not found but this time it just says class not found and hence i am lost as to which class its not able to find.

also where am i suppose to place the hibernate.cfg.xml  file and how does the app know where to pick it from.

this is how I am calling hibernate.

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            log.info("inside 3");
            session.beginTransaction();
            log.info("inside 4");
            session.save(hotelDTO);
            log.info("inside 5");
            session.getTransaction().commit();
            log.info("inside 6");
            HibernateUtil.getSessionFactory().close();
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>But I am getting a NoClassDefFoundError.

Please post full stack trace
Avatar of muthiahmerchant
muthiahmerchant

ASKER

java.lang.ExceptionInInitializerError
      com.myapp.database.HibernateUtil.<clinit>(HibernateUtil.java:17)
      com.myapp.hotel.Hotel.insertHotel(Hotel.java:81)
      com.myapp.hotel.HotelFormAction.execute(HotelFormAction.java:57)
      org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
      org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

this is my HibernateUtil.java

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = 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;
    }

}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
its not in the classpath. does the classpath need to be something like c:/hibernate.cfg.xml or just c:/ is enough if i have hte file in c:/

Thanks
>>just c:/ is enough if i have hte file in c:/

Yes - C:\ should be part, or the whole of the classpath, depending on the rest of the app

I am still getting the same error, but in a different location

java.lang.NoClassDefFoundError
      com.myapp.hotel.Hotel.insertHotel(Hotel.java:81)
      com.myapp.hotel.HotelFormAction.execute(HotelFormAction.java:57)
      org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
      org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)


this is the method in hotel.java and line 81 is Session session = HibernateUtil.getSessionFactory().getCurrentSession();

 public int insertHotel(HotelDTO hotelDTO) throws SQLException{
        log.info("inside insertHotel");
       
        try{
            log.info("inside 1");
            hotelDTO.setHotelId(DBStuff.getPrimaryKey("hotelId","TOUR_HOTELS"));
            log.info("inside 2");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            log.info("inside 3");
            session.beginTransaction();
            log.info("inside 4");
            session.save(hotelDTO);
            log.info("inside 5");
            session.getTransaction().commit();
            log.info("inside 6");
            HibernateUtil.getSessionFactory().close();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
           
        }
        return hotelDTO.getHotelId();
    }
YOu need to put your Hotel class in the classpath too
what?? does that mean that i will have to have all the classes in the class path. does this mean that if i have the following folders then i have to specify each of them in teh classpath.

com.myapp.struts.a, com.myapp.struts.b, com.myapp.struts.c

is there a better way of doing this.

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was able to get it to run, I guess I was missing some jars. but now I don't get any errors, but I don't see the record in the database either. Also for now I hardcoded the location of hibernate.cfg.xml.

This is what my hibernate.cfg.xml looks like
<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
      <property name="hibernate.connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password">xxx</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.SQLServerDialect </property>
      <property name="hibernate.hbm2ddl.auto">create</property>
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

and this is the code that inserts calls the save method

try{
      // This step will read hibernate.cfg.xml and prepare hibernate for use
         File f = new File("C:\\MyProjects\\hibernateTutorial\\hibernate.cfg.xml");
      SessionFactory sessionFactory = new Configuration().configure(f).buildSessionFactory();
       session =sessionFactory.openSession();
        //Create new instance of Contact and set values in it by reading them from form object
         System.out.println("Inserting Record");
        Contact contact = new Contact();
        contact.setId(3);
        contact.setFirstName("Deepak");
        contact.setLastName("Kumar");
        contact.setEmail("deepak_38@yahoo.com");
        session.save(contact);
        System.out.println("Done");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      // Actual contact insertion will happen at this step
      session.flush();
      session.close();
      }

Thanks for all the help
I added a transaction start and end after save and it worked fine.

Thanks
Muthiah
can you folks suggest a good book for hibernate for beginners.