Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA ClassFormat Error "Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence"

I get error from eclipse when I try to invoke a 100% working code. It is for example working in my netbeans but not this eclipse project. The error is absurd and I am almost sure it's caused by some Maven dependency for OPEN JPA that I'm using. Any pointers?

Map<String,String> properties = new HashMap<String,String>();
properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "");
properties.put(PersistenceUnitProperties.JDBC_USER, "root");
properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://localhost:3306/mydb");
properties.put(PersistenceUnitProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");

emf = Persistence.createEntityManagerFactory("Persistentunitname", properties);

The error occurs on the last line, and the error is:

ClassFormat Error "Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence"

like image 410
SQC Avatar asked Dec 13 '11 01:12

SQC


2 Answers

If you have a javaee dependency in your pom like

 <dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>6.0</version>
</dependency>

move it to the end of your dependencies. Your JPA dependency must come before the javaee dependency or you will get that error.

like image 146
Michael Munsey Avatar answered Sep 21 '22 23:09

Michael Munsey


What's happening is that your pom references javaee-api. This package doesn't provide method bodies, just headers. It's effectively a broken package that is 'fixed' at runtime when deployed to a JavaEE environment.

NetBeans is providing a real implementation of javaee whereas Eclipse is not. To solve this add:

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>eclipselink</artifactId>
   <version>2.4.0</version>
   <scope>compile</scope>
</dependency>

This will provide the necessary implementations of javax.persistence and your code will work.

EDIT: (updated the missing artifact)

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.0</version>
</dependency>

pick the latest dependency from here

like image 36
Mark Robinson Avatar answered Sep 25 '22 23:09

Mark Robinson