2012-11-02 111 views
1

ClassFormatError:在方法缺席代碼屬性,是不是在類文件本地或抽象的javax /持久/ InheritanceType冬眠buildSessionFactory ClassFormatError:

我得到這個錯誤時,嘗試構建SessionFactory的 我hibernate.cfg.xml文件:

<?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.bytecode.use_reflection_optimizer">false</property> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.username">mateusz</property> 
     <property name="hibernate.connection.password">mateusz123</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/carpool</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="show_sql">true</property> 
     <mapping class="org.mathew.data.User"></mapping> 
    </session-factory> 
</hibernate-configuration> 

我HibernateUtil類:

public class HibernateUtil { 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      return new AnnotationConfiguration().configure().buildSessionFactory(); 

     } 
     catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public static void shutdown() { 
     // Close caches and connection pools 
     getSessionFactory().close(); 
    } 

} 

堆棧跟蹤:

Exception in thread "main" java.lang.ExceptionInInitializerError 
    at org.mathew.hibutil.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) 
    at org.mathew.hibutil.HibernateUtil.<clinit>(HibernateUtil.java:8) 
    at org.mathew.mysql.MySqlQueries.<init>(MySqlQueries.java:16) 
    at org.mathew.test.AppMain.main(AppMain.java:8) 
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/InheritanceType 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    at org.hibernate.cfg.InheritanceState.extractInheritanceType(InheritanceState.java:51) 
    at org.hibernate.cfg.InheritanceState.<init>(InheritanceState.java:21) 
    at org.hibernate.cfg.AnnotationBinder.buildInheritanceStates(AnnotationBinder.java:2146) 
    at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:492) 
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:277) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319) 
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915) 
    at org.mathew.hibutil.HibernateUtil.buildSessionFactory(HibernateUtil.java:13) 
    ... 3 more 

我的圖書館:

cglib 
commons-collections 
commons-logging 
dom4j 
hibernate3 
hibernate-annotations 
hibernate-commons-annotations 
hibernate-core 
java-api-6.0 
javassist 
jta 
log4j 
mysql-connector-java 
persistence-api 
slf4j 
slf4j-log4j 

我不能上傳圖像:)

,我會心存感激,如果有人可以提供幫助。 感謝! :)

+0

發佈確切的異常_and_ stacktrace的初學者。但無論如何,它看起來像一個依賴問題,在你的類路徑中有多個版本的jar。 –

+0

好吧,我發佈stacktrace :) – Mathew1990

+0

我添加了persistence-api依賴關係,並出現相同的錯誤 – Mathew1990

回答

0

你似乎手動管理你的依賴關係,你應該真的使用依賴管理器(maven,常春藤與螞蟻等),以確保你得到正確的依賴關係(所有庫在預期的版本)。既然你不指定所使用的庫的版本,有幾個可能的問題:

  • 持久性API的版本不兼容使用Hibernate
  • 休眠罐子的不兼容組合:你顯然有Hibernate3的jar和hibernate-core一樣,這看起來不正確。它看起來像是一個老版本的Hibernate混合了更新的jar。因此,只有在最新版本中才存在的類可能會從舊類訪問類,如果該jar在類路徑中首先出現的話。