线程" main"中的例外情况java.lang.ClassFormatError:... NON Maven项目

时间:2015-06-23 12:30:29

标签: java persistence.xml

Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at com.bt.niab.ejbservice.junit.DBTest.init(DBTest.java:51)
    at com.bt.niab.ejbservice.junit.DBTest.main(DBTest.java:28)

请帮助解决此错误。我尝试添加和删除其他帖子中提到的几个jar但它没有帮助...调试异常时抛出:EntityManagerFactory factory = Persistence.createEntityManagerFactory(" testdb");

的persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0"
        xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="testDB" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <!--  <jta-data-source>com.bt.niab.jdbc.datasource.niabdl</jta-data-source> -->
            <class> (Cannot mention here) </class>

            <properties>
                <property name="javax.persistence.jdbc.url" value="db_url (Cannot mention here)" />
                <property name="javax.persistence.jdbc.user" value="user" />
                <property name="javax.persistence.jdbc.password" value="pass" />
                <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" /> 

                <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.format_sql" value="true" />
                <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory" /> 
            </properties>
        </persistence-unit>
    </persistence>

DBTest.java

package com.bt.niab.ejbservice.junit;    
import java.util.ArrayList;    
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class DBTest {   

    EntityManagerFactory factory; 
    EntityManager manager;  
    EntityTransaction tx;       

    public static void main(String[] args)
    {
        DBTest obj = new DBTest();

        try
        {       
            obj.init();
            obj.search();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            obj.shutdown();
        }           
    }

    private void shutdown()
    {    
        System.out.println("Inside Shutdown method");
        manager.close();      
        factory.close();
    }

    public void init()
    {
        System.out.println("Inside init method");
        factory = Persistence.createEntityManagerFactory("IonixDLUnit"); 
        manager = factory.createEntityManager();
        System.out.println("After init method");        
    }

    private void search() 
    { 
        System.out.println("Inside Search method");
        ArrayList<DBTest> arrayList = new ArrayList<DBTest>();
        tx = manager.getTransaction();
        tx.begin();
        Query query = manager.createQuery("SELECT *  FROM address");
        if(query != null)
        {
            arrayList = (ArrayList<DBTest>) query.getResultList();
            for(DBTest p : arrayList) 
            {         
                System.out.println(p);  
            }
        }
        tx.commit();
    }
}

文件夹结构

JavaProject
   |---src
     |--- package
            |--- DBTest.java
     |--- package
     |--- META-INF
              |--- MANIFEST.MF
              |--- persistence.xml
              |--- jndi.properties

0 个答案:

没有答案