下面代码运行时报异常:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.HibernateUtils
at com.Test.main(Test.java:28)究竟怎么回事啊???public class Test {
public static void main(String[] args) {

Session session=null;
Transaction tx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();

Group group=new Group();
group.setName("管理员组");

session.save(group);

tx.commit();
}catch(Exception ex){
tx.rollback();
ex.printStackTrace();

}finally{
HibernateUtils.closeSession(session);
}


}}
HibernateUtils:public class HibernateUtils { private static final SessionFactory factory;
static{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}

public static SessionFactory getSessionFactory(){
return factory;
}

public static Session getSession(){
return factory.openSession();
}

public static void  closeSession(Session session){
if(session != null){
if(session.isOpen()){
session.close();
}
}
}
}

解决方案 »

  1.   

    不是的,看错了,不好意。HibernateUtils你编译了吗,在编译路经下吗?
      

  2.   

    问题是HibernateUtils不能正常的初始化,单看代码没什么问题,是不是hibernate的配置文件的配置有问题啊?
      

  3.   

    hibernate配置文件如下:<?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
         <property name="hibernate.connection.url">jdbc:mysql://localhost/h_test</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">sa</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create</property>

    <mapping resource="com/Group.hbm.xml"/>
    <mapping resource="com/User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
      

  4.   

    private static final SessionFactory factory;
    去掉final 看看行不行