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

SQC picture SQC · Dec 13, 2011 · Viewed 11k times · Source

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"

Answer

Michael Munsey picture Michael Munsey · Apr 14, 2014

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.