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();
LVL 1
muthiahmerchantAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Are you making the hibernate.cfg.xml available in the classpath
0
 
CEHJCommented:
>>But I am getting a NoClassDefFoundError.

Please post full stack trace
0
 
muthiahmerchantAuthor Commented:
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;
    }

}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
muthiahmerchantAuthor Commented:
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
0
 
CEHJCommented:
>>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

0
 
muthiahmerchantAuthor Commented:
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();
    }
0
 
CEHJCommented:
YOu need to put your Hotel class in the classpath too
0
 
muthiahmerchantAuthor Commented:
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
0
 
objectsConnect With a Mentor Commented:
No you don't need to put every directory in your classpath
You need to make all classes available to the classpath. For classes that are in a package you need to add the pparent directory of the package hirarchy root to tjhe classpatth

eg. if your classes are in:

c:\classes\com\myapp\struts\a
c:\classes\com\myapp\struts\b
c:\classes\com\myapp\struts\c

then you would just add c:\classes to your classpath
and also copy hibernate.cfg to c:\classes

Thopugh if this is a struts applicatiuon then you do not need to worry and instead put all your classes in <webapp>/WEB-INF/classes (or jar them up and put them in lib).

Follwoing will give you some more infor on classpaths

http://mindprod.com/jgloss/classpath.html
0
 
muthiahmerchantAuthor Commented:
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
0
 
muthiahmerchantAuthor Commented:
I added a transaction start and end after save and it worked fine.

Thanks
Muthiah
0
 
muthiahmerchantAuthor Commented:
can you folks suggest a good book for hibernate for beginners.
0
 
CEHJCommented:
0
All Courses

From novice to tech pro — start learning today.