请教这个错误是怎么回事?
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();
}
}
}
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();
}
}
}
是否存在编译文件
比较笨的办法是找个可用的jar用rar打开,直接把你的类拷贝到相关路径
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,不太懂。多谢
还有myeclipse直接在你的源文件的目录,右键 --》export-->java--> jar file即可