Solved

Hibernate java.lang.NoClassDefFoundError

Posted on 2006-06-12
13
768 Views
Last Modified: 2013-11-24
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();
0
Comment
Question by:muthiahmerchant
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16888122
>>But I am getting a NoClassDefFoundError.

Please post full stack trace
0
 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16888220
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 16888257
Are you making the hibernate.cfg.xml available in the classpath
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16888321
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16888465
>>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
 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16888761
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16888855
YOu need to put your Hotel class in the classpath too
0
 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16888952
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
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 16889963
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
 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16898483
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
 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16898509
I added a transaction start and end after save and it worked fine.

Thanks
Muthiah
0
 
LVL 1

Author Comment

by:muthiahmerchant
ID: 16898512
can you folks suggest a good book for hibernate for beginners.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16900256
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question