-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: [Hibernate/Struts] NoDefClassFoundError
PostPosted: Thu Dec 23, 2004 10:22 am 
Newbie

Joined: Mon Oct 18, 2004 11:27 am
Posts: 4
Hibernate version:
2.1 in MyEclipse

Name and version of the database you are using:
MySQL 4.17

I'm building my first Struts/Tiles/Hibernate application and ran into some problems.

I have a Businessclass that has a method for a List, I call on that method straight in my jsp. This same approach did work on another tutorial I was working on, but in my project now it isn't.

Here is the method in CustomerDAO.java:
Code:
public List getCustomerList()
   {
      /*
       * Use the ConnectionFactory to retrieve an open
       * Hibernate Session.
       *
       */
      Session session = null;

      try
      {
         session = HibernateUtil.getSession();
         /*
         * Build HQL (Hibernate Query Language) query to retrieve a list
         * of all the items currently stored by Hibernate.
          */
         Query query = session.createQuery("select Customer from com.frontier.hibernate.Customer Customer order by Customer.customerid");
         return query.list();

      }
      catch (HibernateException e)
      {
         System.err.println("Session not getted!" + e.getMessage());
         throw new RuntimeException(e);
      }
      /*
       * Regardless of whether the above processing resulted in an Exception
       * or proceeded normally, we want to close the Hibernate session.  When
       * closing the session, we must allow for the possibility of a Hibernate
       * Exception.
       *
       */
      finally
      {
         if (session != null)
         {
            try
            {
               session.close();
            }
            catch (HibernateException e)
            {
               System.err.println("Hibernate Exception" + e.getMessage());
               throw new RuntimeException(e);
            }

         }
      }
   }


This is my accOptions.jsp:
Code:
<%@ page import="com.frontier.hibernate.CustomerDAO" %>
<%@ page import="java.util.List" %>
<%
      /*
       * This code will generate a list of objects from the
       * database and place a reference to this list in the
       * request object.
       *
       */
      List customerList = CustomerDAO.getInstance().getCustomerList();
     
      request.setAttribute("customers", customerList);
     
   %>
<logic:iterate id="element" name="customers" scope="request" type="com.frontier.hibernate.Customer" indexId="toggle">
   <table>
   <tr><td>Name:</td><td><bean:write name="element" property="name" scope="request" /></td></tr>
   <tr><td>Address:</td><td><bean:write name="element" property="address" scope="request" /></td></tr>
   <tr><td>City:</td><td><bean:write name="element" property="city" scope="request" /></td></tr>
   <tr><td>Number of Mortgages:</td><td><bean:write name="element" property="noofmortgages" scope="request"  /></td></tr>
    </table>
</logic:iterate>


And this is my rather long (sorry for that) HibernateUtil.java:
Code:
/**
* Basic Hibernate helper class, handles SessionFactory, Session and Transaction.
* <p>
* Uses a static initializer for the initial SessionFactory creation
* and holds Session and Transactions in thread local variables. All
* exceptions are wrapped in an unchecked InfrastructureException.
*
* @author christian@hibernate.org
*/
public class HibernateUtil {

   private static Log log = LogFactory.getLog(SessionFactory.class);

   private static Configuration configuration;
   private static SessionFactory sessionFactory;
   private static final ThreadLocal threadSession = new ThreadLocal();
   private static final ThreadLocal threadTransaction = new ThreadLocal();
   private static final ThreadLocal threadInterceptor = new ThreadLocal();

   // Create the initial SessionFactory from the default configuration files
   static {
      try {
         //configuration = new Configuration();
         sessionFactory = new Configuration().configure().buildSessionFactory();
         // We could also let Hibernate bind it to JNDI:
         // configuration.configure().buildSessionFactory()
      } catch (Throwable ex) {
         // We have to catch Throwable, otherwise we will miss
         // NoClassDefFoundError and other subclasses of Error
         log.error("Building SessionFactory failed.", ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   
   /**
    * Returns the SessionFactory used for this static class.
    *
    * @return SessionFactory
    */
   public static SessionFactory getSessionFactory() {
      
      /* Instead of a static variable, use JNDI:
      SessionFactory sessions = null;
      try {
         Context ctx = new InitialContext();
         String jndiName = "java:hibernate/HibernateFactory";
         sessions = (SessionFactory)ctx.lookup(jndiName);
      } catch (NamingException ex) {
         throw new InfrastructureException(ex);
      }
      return sessions;
      */
      return sessionFactory;
   }

   /**
    * Returns the original Hibernate configuration.
    *
    * @return Configuration
    */
   public static Configuration getConfiguration() {
      return configuration;
   }

   /**
    * Rebuild the SessionFactory with the static Configuration.
    *
    */
    public static void rebuildSessionFactory()
      throws InfrastructureException {
      synchronized(sessionFactory) {
         try {
            sessionFactory = getConfiguration().buildSessionFactory();
         } catch (Exception ex) {
            throw new InfrastructureException(ex);
         }
      }
    }

   /**
    * Rebuild the SessionFactory with the given Hibernate Configuration.
    *
    * @param cfg
    */
    public static void rebuildSessionFactory(Configuration cfg)
      throws InfrastructureException {
      synchronized(sessionFactory) {
         try {
            sessionFactory = cfg.buildSessionFactory();
            configuration = cfg;
         } catch (Exception ex) {
            throw new InfrastructureException(ex);
         }
      }
    }

   /**
    * Retrieves the current Session local to the thread.
    * <p/>
    * If no Session is open, opens a new Session for the running thread.
    *
    * @return Session
    */
   public static Session getSession()
      throws InfrastructureException {
      Session s = (Session) threadSession.get();
      try {
         if (s == null || s.isOpen() == false) {
            log.debug("Opening new Session for this thread.");
            if (getInterceptor() != null) {
               log.debug("Using interceptor: " + getInterceptor().getClass());
               s = getSessionFactory().openSession(getInterceptor());
            } else {
               s = getSessionFactory().openSession();
            }
            threadSession.set(s);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return s;
   }

   /**
    * Closes the Session local to the thread.
    */
   public static void closeSession()
      throws InfrastructureException {
      try {
         Session s = (Session) threadSession.get();
         threadSession.set(null);
         if (s != null && s.isOpen()) {
            log.debug("Closing Session of this thread.");
            s.close();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Start a new database transaction.
    */
   public static void beginTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if (tx == null) {
            log.debug("Starting new database transaction in this thread.");
            tx = getSession().beginTransaction();
            threadTransaction.set(tx);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void commitTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if ( tx != null && !tx.wasCommitted()
                     && !tx.wasRolledBack() ) {
            log.debug("Committing database transaction of this thread.");
            tx.commit();
         }
         threadTransaction.set(null);
      } catch (HibernateException ex) {
         rollbackTransaction();
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void rollbackTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         threadTransaction.set(null);
         if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
            log.debug("Tyring to rollback database transaction of this thread.");
            tx.rollback();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      } finally {
         closeSession();
      }
   }

   /**
    * Reconnects a Hibernate Session to the current Thread.
    *
    * @param session The Hibernate Session to be reconnected.
    */
   public static void reconnect(Session session)
      throws InfrastructureException {
      try {
         session.reconnect();
         threadSession.set(session);
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Disconnect and return Session from current Thread.
    *
    * @return Session the disconnected Session
    */
   public static Session disconnectSession()
      throws InfrastructureException {

      Session session = getSession();
      try {
         threadSession.set(null);
         if (session.isConnected() && session.isOpen())
            session.disconnect();
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return session;
   }

   /**
    * Register a Hibernate interceptor with the current thread.
    * <p>
    * Every Session opened is opened with this interceptor after
    * registration. Has no effect if the current Session of the
    * thread is already open, effective on next close()/getSession().
    */
   public static void registerInterceptor(Interceptor interceptor) {
      threadInterceptor.set(interceptor);
   }

   private static Interceptor getInterceptor() {
      Interceptor interceptor =
         (Interceptor) threadInterceptor.get();
      return interceptor;
   }
   
}


Now when I call on the jsp page the first time I get:
Code:
exception

javax.servlet.ServletException
   org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:845)
   org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:778)
   org.apache.jsp.jsp.accOptions_jsp._jspService(org.apache.jsp.jsp.accOptions_jsp:146)
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.ExceptionInInitializerError
   com.frontier.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:39)
   com.frontier.hibernate.CustomerDAO.getCustomerList(CustomerDAO.java:195)
   org.apache.jsp.jsp.accOptions_jsp._jspService(org.apache.jsp.jsp.accOptions_jsp:80)
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


When I refresh that same page i get same exception but different root cause:
Code:
root cause

java.lang.NoClassDefFoundError
   com.frontier.hibernate.CustomerDAO.getCustomerList(CustomerDAO.java:195)
   org.apache.jsp.jsp.accOptions_jsp._jspService(org.apache.jsp.jsp.accOptions_jsp:80)
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


I've googled around, found things about classpaths, libraries and such but as far as I can see all is well.
I've got hibernate2.jar AND cglib-full-2.0.1.jar in my WEB-INF/lib.
In my java files I'm referering to the right classes (otherwise Eclipse would warn me about it), so I'm frustrated out of idea's!

Can anyone help me?
O yeah, using JDK 1.5, Tomcat 5.5.4 and Eclipse/MyEclipse.

Another thing. The first rootcause with line 39 is from the throw/catch at wanting to build a SessionFactory and the second one at CustomerDAO:195 is at the List method at trying to get the HibernateUtil.getSession


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 11:10 am 
Newbie

Joined: Mon Oct 18, 2004 11:27 am
Posts: 4
Does anybody have any idea on this? I still have the problem


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 11:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
java.lang.ExceptionInInitializerError is often caused because of an exception thrown in static blocks or initializaiton of static fields.

If such an exception occurrs it will happen at classload time and thus afterwards you will get a NoClassDefFund error

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.