2014-03-24 54 views
3

我創建了一個web動態項目,並且想要使用Hibernatejava.lang.NoClassDefFoundError:org/hibernate/cfg/AnnotationConfiguration

然後,我用Hibernate Code Generation來生成類代碼,並且我創建了一個類HibernateUtil,其中我初始化了SessionFactory

public class HibernateUtil { 
    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory(){ 
     try{ 
      SessionFactory sessionFactory = 
        new AnnotationConfiguration().configure().buildSessionFactory(); 
      return sessionFactory; 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public static void shutdown() { 
     getSessionFactory().close(); 
    } 
} 

而另一個類HibernateSessionFactoryListener

public class HibernateSessionFactoryListener implements ServletContextListener{ 

    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     System.out.println("\n Context Initilaise \n"); 
    } 

    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 
     HibernateUtil.shutdown(); 
     System.out.println("\n Context Detruit \n"); 
    } 

} 

當我跑我的項目,我收到此錯誤信息:

java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration 
    at com.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:28) 
    at com.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:11) 
    at com.hibernate.HibernateSessionFactoryListener.contextInitialized(HibernateSessionFactoryListener.java:12) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569) 
    ... 12 more 

我怎樣才能解決這個問題?

編輯1:

這是我的Java構建路徑:

enter image description here

+0

你有在類路徑中的類? – ArunM

+0

@ArunM不,我沒有 –

+0

PLease將hibernate jar添加到你的類路徑中。這應該可以解決問題。哪個是你正在使用的hibernate的版本? – ArunM

回答