7

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"

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
SQC
  • 591
  • 3
  • 9
  • 18
  • See http://maven.40175.n5.nabble.com/Problem-running-unit-tests-from-maven-JPA-related-td126893.html for a solution recommendation. – Sri Sankaran Dec 13 '11 at 02:29
  • The recommended solution there , openejb -javaee 6.0 , doesnot exist (at least not version 6). I think the problem has a similar solution but can't seem to find it – SQC Dec 13 '11 at 02:44
  • I have javaee6.0.jar , so I think there's something else going on – SQC Dec 13 '11 at 02:54
  • 1
    Are you saying that you are getting the problem at **compile** time? If so my reference above is not applicable. That addresses **runtime** problems. Also the solution there (although over 2 years old) points to a version 5.0-1 of the javaee API. I think you can get by with any current JavaEE container. – Sri Sankaran Dec 13 '11 at 03:00
  • 2
    Update your post with relevant portions of your `pom.xml`. – Sri Sankaran Dec 13 '11 at 03:02
  • No I get errors during runtime only. Regarding the pom, I'm not sure which portion is relevant as there is no javaee explicitely mentioned but looking at my project jars I can see javaee6.0 . – SQC Dec 13 '11 at 03:52
  • Problem fixed. Thanks Sri. I added a maven dependency (glassfish openjpa). – SQC Dec 13 '11 at 19:55

4 Answers4

14

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.

Michael Munsey
  • 3,740
  • 1
  • 25
  • 15
3

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

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
Mark Robinson
  • 3,135
  • 1
  • 22
  • 37
2

Another alternative without change the javaee api, it's to declare the real api we need first in the maven dependencies block, something like this:

  <dependencies>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>${jpa-api.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>{slf4j.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>${jta.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javaee</groupId>
      <artifactId>javaee-api</artifactId>
      <version>${javaee-api.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- Another tests dependencies-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>${assertj-core.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>${mockito-core.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

With this way, we override the classpath for our test scope. We can read about this maven behavior here and here.

JuanMoreno
  • 2,498
  • 1
  • 25
  • 34
0

Typically it is enough to download the JavaEE JAR directly from Oracle:

http://www.mkyong.com/maven/how-to-download-j2ee-api-javaee-jar-from-maven/

Alessandro Santini
  • 1,943
  • 13
  • 17