-->
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: Strange java.lang.NoClassDefFoundError
PostPosted: Wed Dec 31, 2003 7:33 am 
Newbie

Joined: Mon Dec 29, 2003 10:45 am
Posts: 12
It looks like a hard road getting Hibernate work, but I'm getting closer. Stdout below clearly tells me that Hibernate engine is up and running, but I still get an NoClassDefFoundError with no classname or anything to point me in the right direction. Exception is thrown at MenuItems' row Session session = HibernateUtil.currentSession();. After that the program enters HibernateUtils static-block but the first row throws the exception. This is a long post, I know, but I am stuck:)

thanks,
Jari

HibernateUtil:

Code:
public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure(HibernateUtil.
            class.getClassLoader().getResource("hibernate.cfg.xml")).buildSessionFactory();
        } catch (HibernateException ex) {
            throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    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);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
}


MenuItems:

Code:
public class MenuItems {

    private SessionFactory sessions;

    public Materialclass[] getMaterialclasses() throws Exception {
        try {
            // the row below causes the exception         
            Session session = HibernateUtil.currentSession();
            Materialclass[] classes;
            Transaction tx= session.beginTransaction();

            List list = session.find("from Materialclass matClass order by matClass.definition");
            tx.commit();
            System.out.println("lista: " + list.size());
            classes = (Materialclass[]) list.toArray();
            return classes;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
            return null;
        } finally {
            try {
               HibernateUtil.closeSession();
            } catch (HibernateException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}


Exception:

Code:
2003-12-31 13:11:05 StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
java.lang.NoClassDefFoundError
   at emove2.hibernate.MenuItems.getMaterialclasses(MenuItems.java:34)
   at emove2.struts.action.MenuAction.initReceiverMenu(MenuAction.java:29)
   at emove2.struts.action.MenuAction.execute(MenuAction.java:18)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:594)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
   at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:805)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:696)
   at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
   at java.lang.Thread.run(Unknown Source)


Stdout:

Code:
13:11:03,453  INFO Environment:432 - Hibernate 2.1.1
13:11:03,468  INFO Environment:461 - hibernate.properties not found
13:11:03,468  INFO Environment:481 - using CGLIB reflection optimizer
13:11:03,484  INFO Configuration:858 - configuring from url: file:/E:/Apache/Tomcat50/work/Catalina/localhost/emove2/WEB-INF/classes/hibernate.cfg.xml
13:11:03,609 DEBUG DTDEntityResolver:20 - trying to locate http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd in classpath under net/sf/hibernate/
13:11:03,812 DEBUG DTDEntityResolver:29 - found http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd in classpath
13:11:03,890 DEBUG Configuration:801 - connection.datasource=java:comp/env/jdbc/emove2DS
13:11:03,906 DEBUG Configuration:801 - show_sql=false
13:11:03,906 DEBUG Configuration:801 - use_outer_join=true
13:11:03,906 DEBUG Configuration:801 - transaction.factory_class=net.sf.hibernate.transaction.JTATransactionFactory
13:11:03,906 DEBUG Configuration:801 - dialect=net.sf.hibernate.dialect.Oracle9Dialect
13:11:03,906 DEBUG Configuration:952 - null<-org.dom4j.tree.DefaultAttribute@1759d12 [Attribute: name resource value "hibernate.hbm.xml"]
13:11:03,906  INFO Configuration:300 - Mapping resource: hibernate.hbm.xml
13:11:03,921 DEBUG DTDEntityResolver:20 - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath under net/sf/hibernate/
13:11:03,921 DEBUG DTDEntityResolver:29 - found http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath
13:11:04,093  INFO Binder:225 - Mapping class: emove2.hibernate.model.Materialclass -> EMOVE_CODE_MATERIALCLASS
13:11:04,234 DEBUG Binder:449 - Mapped property: uid -> uid, type: integer
13:11:04,281 DEBUG Binder:449 - Mapped property: codeGroup -> codeGroup, type: string
13:11:04,281 DEBUG Binder:449 - Mapped property: definition -> definition, type: string
13:11:04,281 DEBUG Binder:449 - Mapped property: materialclass -> materialclass, type: big_decimal
13:11:04,281  INFO Configuration:998 - Configured SessionFactory: null
13:11:04,281 DEBUG Configuration:999 - properties: { lots of properties...}
13:11:04,546  INFO Configuration:584 - processing one-to-many association mappings
13:11:04,546  INFO Configuration:593 - processing one-to-one association property references
13:11:04,546  INFO Configuration:618 - processing foreign key constraints
13:11:04,593  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.Oracle9Dialect
13:11:04,593  INFO SettingsFactory:62 - Use outer join fetching: true
13:11:04,625  INFO NamingHelper:26 - JNDI InitialContext properties:{}
13:11:04,656  INFO DatasourceConnectionProvider:51 - Using datasource: java:comp/env/jdbc/emove2DS
13:11:04,671  INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
13:11:04,671  INFO NamingHelper:26 - JNDI InitialContext properties:{}
13:11:04,687  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
13:11:04,687  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool@694f12)
   LogAbandoned: false
   RemoveAbandoned: true
   RemoveAbandonedTimeout: 30
13:11:05,312  INFO SettingsFactory:89 - Use scrollable result sets: true
13:11:05,312  INFO SettingsFactory:90 - JDBC 2 max batch size: 15
13:11:05,312  INFO SettingsFactory:99 - Query language substitutions: {}
13:11:05,312  INFO SettingsFactory:110 - cache provider: net.sf.ehcache.hibernate.Provider



Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 7:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I think you are still missing required hibernate jars. Please check the readme file in the lib directory and check if you have all required jars in the classpath. This is hard to tell without info which class is not found.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 8:04 am 
Newbie

Joined: Mon Dec 29, 2003 10:45 am
Posts: 12
gloeglm wrote:
I think you are still missing required hibernate jars. Please check the readme file in the lib directory and check if you have all required jars in the classpath.


They are all there in the war-file, all that say required, standalone or not.

gloeglm wrote:
This is hard to tell without info which class is not found.


Believe me, I know...

Jari


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 8:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Try to move you static block into a plain non static method (use the singleton pattern with lazy initialization). There is probably a classloader problem.

Code:
public static MyClass getInstance() {
  if (_instance == null) {
    _instance = new MyClass();
  }
  return _instance;
}

public MyClass() {
  //init Hibernate
}
}

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 9:10 am 
Newbie

Joined: Mon Dec 29, 2003 10:45 am
Posts: 12
Thanks, that helped, I was missing (optional?) ehcache.jar. Now I have some new exceptions but I'll try to figure them out by myself first.

Happy New Year,
Jari


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 9:14 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
The class name can be "HibernateUtil".

If this code fails:

static {
try {
sessionFactory = new Configuration().configure(HibernateUtil.
class.getClassLoader().getResource("hibernate.cfg.xml")).buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}

it will produce "ExceptionInInitializerError" caused by "RuntimeException" caused by "HibernateException".
It can be thrown only once, next time JVM will not try to call <clinit>, it will mark class as dead and will thow CNF.
It must be better to configure hibernate in Servlet.init(), it will help to find problems faster, Servlet.destroy() can be used to close pool too.


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.