Solved

Hibernate java.lang.NoClassDefFoundError

Posted on 2006-06-12
13
755 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 58
Core Java. What output will be and why ? 6 58
JList custom Cell Renderer refresh 15 41
Java Timer (static) 9 12
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now