-->
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.  [ 6 posts ] 
Author Message
 Post subject: NoClassFoundDefError
PostPosted: Mon Aug 02, 2004 6:45 am 
Newbie

Joined: Mon Aug 02, 2004 5:33 am
Posts: 4
Hi
I'm new to Hibernate. I'm using Hibernate 2.1.3, Mysql 4.0.12 and Tomcat 5.

I'm using the example HibernateUtilities class and my DAO class uses it to get the current session.

Here's the HibernateUtilities class:

Code:

package com.cms.server.util;


import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

/**
*
* @author
*
*/
public class HibernateUtil
{
   private static final SessionFactory sessionFactory;
   public static final ThreadLocal session = new ThreadLocal();

   static
   {
      try
      {
         // Create the SessionFactory
         sessionFactory = new Configuration().configure().buildSessionFactory();
      }
      catch (HibernateException ex)
      {
         throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
      }
   }

   
   public static Session currentSession() throws HibernateException
   {
      Session s = (Session) session.get();
      // Open a new Session, if this Thread has none yet
      if (s == null)
      {
         s = sessionFactory.openSession();
         session.set(s);
         System.out.println("creating sessionFactory");
      }
      System.out.println("Returning session.");
      return s;
   }

   public static void closeSession() throws HibernateException
   {
      System.out.println("HibernateUtil.closeSession: inside the method.");
      try
      {
         Session s = (Session) session.get();
         session.set(null);
         if (s != null)
            s.close();
      }
      catch(Throwable t){
         t.printStackTrace();
      }
   }

}


I'm getting NoClassDefFoundError when I run my test servlet that uses the DAO.

Here's the stack trace:

2004-08-02 10:41:52 StandardWrapperValve[TestHibernateServlet]: Servlet.service() for servlet TestHibernateServlet threw exception
java.lang.NoClassDefFoundError
at com.cms.server.database.hibernate.HibernateUserDAO.getUserByUsernamePassword(Unknown Source)
at com.cms.server.servlet.TestHibernateServlet.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:793)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
at java.lang.Thread.run(Thread.java:536)

I have set up my Classpath as in the qucikstart guide.

Thanks for any help

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 6:47 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There was an error in the Quickstart not so long ago, please use the following static initializer in HibernateUtil:

Code:
   // Create the initial SessionFactory from the default configuration files
   static {
      try {
         configuration = new Configuration();
         sessionFactory = 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);
      }
   }

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Not sure what the difference is..?
PostPosted: Mon Aug 02, 2004 7:57 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
Hi Chritian,
I'm not sure what the difference is between the static iniitializer you posted and the one that Su posted?

As far as I can see the only difference is that the one that you posted catches Errors as well as Exceptions..

Is the idea to get the stack trace from inside the static initializer and try and find out whats going from from there.

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 7:58 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The idea is to catch Errors instead of Exceptions. Read the comment...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 11:44 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
I did. I have a similar error. My point was that the code doesnt solve the problem, it just allows you to catch the NoClassDefFoundError and do *something* with it.

Let me rephrase: have you any ideas as to what might be causing the Error in the first place?

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 11:46 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Guys, its so trivial: The catch allows you to _SHOW_ the error that was the cause of the problem. It will get swallowed otherwise.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.