请教这个错误是怎么回事?
java.lang.NoClassDefFoundError: Could not initialize class com.demo.hibernate.util.HibernateSessionFactory
HibernateSessionFactory.java代码:
package com.demo.hibernate.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.*;public class HibernateSessionFactory { private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
 private static final ThreadLocal<Session> threadLocal= new ThreadLocal<Session>();
 private static final Configuration cfg = new Configuration();
 private static org.hibernate.SessionFactory sessionFactory;
 
 public static Session currentSession() throws HibernateException{
  Session session=(Session)threadLocal.get();
  if(session == null){
   if(sessionFactory == null){
    try{
     cfg.configure(CONFIG_FILE_LOCATION);
     sessionFactory=cfg.buildSessionFactory();
    }
    catch(Exception e){
     System.out.println("%%%% Error Creating SessionFactory %%%%");
     e.printStackTrace();
    }
   }
   session=sessionFactory.openSession();
   threadLocal.set(session);
  }
  return session;
 }
 
 public static void closeSession() throws HibernateException{
  Session session=(Session)threadLocal.get();
     threadLocal.set(null);
  if(session != null){
   session.close();
  }
 }
}

解决方案 »

  1.   

    类没有编译完成,检查下你的class/com/demo/hibernate/util
    是否存在编译文件
      

  2.   

    我把那个hibernate项目输出了一个jar包,在里面有HibernateSessionFactory的class文件,在struts项目中调用,就出现了以上那个问题,很怪异
      

  3.   

    那说明你得jar包导出有问题呗。
    比较笨的办法是找个可用的jar用rar打开,直接把你的类拷贝到相关路径
      

  4.   

    你在什么地方,用什么代码初始化这个HibernateSessionFactory?
      

  5.   

    下面是调用HibernateSessionFactory的模块,
    package com.demo.hibernate.dao;
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;import com.demo.hibernate.beans.*;
    import com.demo.hibernate.util.HibernateSessionFactory;public class UserDAOImpl implements UserDAO{ public DemoUser select(String username){
    Session session=null;
    DemoUser record = null;
    try{
    session=HibernateSessionFactory.currentSession();
    Query query=session.createQuery("from DemoUser where username=?");
    query.setString(0,username.trim());
    record=(DemoUser)query.uniqueResult();
    query =null;
    }catch(HibernateException e){
    throw e;
    }finally{
    HibernateSessionFactory.closeSession();
    }
    return record;
    }
    public Integer insert(DemoUser record){
    Session session = null;
    Transaction tx = null;
    Integer id = null;
    try{
    session = HibernateSessionFactory.currentSession();
    tx=session.beginTransaction();
    session.save(record);
    id=record.getId();
    tx.commit();
    }catch(HibernateException e){
    throw e;
    }finally{
    if(tx!=null){
    tx.rollback();
    }
    HibernateSessionFactory.closeSession();
    }
    return id;
    }
    }
    我也认为是导出jar文件的问题, 不过warison2008可否告知相应的目录是哪,才开始用eclipse,不太懂。多谢
      

  6.   

    晕啦,我说的是用rar打开一个可以用的jar包,然后新建文件相关夹,或者直接把你classes下的文件+路径添加到你的jar包中。
    还有myeclipse直接在你的源文件的目录,右键 --》export-->java--> jar file即可