简体   繁体   English

JPA ClassFormat 错误“类文件 javax/persistence/Persistence 中非本机或抽象的方法中缺少代码属性”

[英]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.当我尝试调用 100% 工作代码时,我从 eclipse 中得到错误。 It is for example working in my netbeans but not this eclipse project.例如,它在我的 netbeans 中工作,但不是这个 eclipse 项目。 The error is absurd and I am almost sure it's caused by some Maven dependency for OPEN JPA that I'm using.这个错误很荒谬,我几乎可以肯定它是由我正在使用的 OPEN JPA 的一些 Maven 依赖引起的。 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" ClassFormat 错误“类文件 javax/persistence/Persistence 中非本机或抽象的方法中缺少代码属性”

If you have a javaee dependency in your pom like如果您的 pom 中有 javaee 依赖项,例如

 <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.您的 JPA 依赖项必须位于 javaee 依赖项之前,否则您将收到该错误。

What's happening is that your pom references javaee-api.发生的事情是您的 pom 引用了 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.它实际上是一个损坏的包,在部署到 JavaEE 环境时会在运行时“修复”。

NetBeans is providing a real implementation of javaee whereas Eclipse is not. NetBeans 提供了真正的 javaee 实现,而 Eclipse 则没有。 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.这将提供 javax.persistence 的必要实现,并且您的代码将起作用。

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这里选择最新的依赖项

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:另一种不改变javaee api的替代方案,它是在maven依赖块中首先声明我们需要的真正api,如下所示:

  <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 .我们可以在此处此处阅读有关此 Maven 行为的信息。

Typically it is enough to download the JavaEE JAR directly from Oracle:通常直接从 Oracle 下载 JavaEE JAR 就足够了:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java EE 6中类文件javax / mail / MessagingException中不是本机或抽象的方法中的缺少Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException in Java EE 6 ClassFormatError:在类文件 javax/mail/MessagingException 中非本机或抽象的方法中缺少代码属性 - ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException 方法中的Absent Code属性在类文件javax / servlet / ServletException中不是本机的或抽象的 - Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException class 文件 javax/validation/ConstraintViolationException 中非本机或抽象方法中的缺失代码属性 - Absent Code attribute in method that is not native or abstract in class file javax/validation/ConstraintViolationException Struts2单元测试给出错误:类文件javax / servlet / ServletException中不是本机或抽象的方法中的缺少Code属性 - Struts2 Unit test gives the error: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException 在类文件javax / faces / webapp / FacesServlet中的非本机或抽象方法中的Absent Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet java.lang.ClassFormatError:类文件javax / faces / FacesException中不是本机或抽象的方法中的缺少Code属性 - java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/FacesException java.lang.ClassFormatError:在类文件 javax/mail/MessagingException 中非本机或抽象的方法中缺少代码属性 - java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException 类文件javax / transaction / SystemException中不是本机或抽象的方法中的缺少Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException java.lang.ClassFormatError:类文件javax / mail / internet / ParseException中不是本机或抽象的方法中的缺少Code属性 - java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/internet/ParseException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM