JPA ClassFormat 错误“类文件 javax/persistence/Persistence 中不是本机或抽象的方法中缺少代码属性”
当我尝试调用 100% 工作代码时,我收到 Eclipse 错误。例如,它在我的 netbeans 中工作,但不在这个 eclipse 项目中工作。该错误很荒谬,我几乎可以肯定它是由我正在使用的 OPEN JPA 的某些 Maven 依赖项引起的。有什么指点吗?
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);
错误发生在最后一行,错误是:
ClassFormat 错误“类文件 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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 pom 中有 javaee 依赖项,
请将其移至依赖项的末尾。您的 JPA 依赖项必须位于 javaee 依赖项之前,否则您将收到该错误。
If you have a javaee dependency in your pom like
move it to the end of your dependencies. Your JPA dependency must come before the javaee dependency or you will get that error.
发生的情况是您的 pom 引用了 javaee-api。该包不提供方法体,仅提供标头。它实际上是一个损坏的包,部署到 JavaEE 环境时会在运行时“修复”。
NetBeans 提供了 javaee 的真正实现,而 Eclipse 则不然。要解决此问题,请添加:
这将提供 javax.persistence 的必要实现,并且您的代码将起作用。
编辑:(更新了丢失的工件)
从中选择最新的依赖项这里
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:
This will provide the necessary implementations of javax.persistence and your code will work.
EDIT: (updated the missing artifact)
pick the latest dependency from here
另一种无需更改 javaee api 的替代方案是首先在 Maven 依赖项块中声明我们需要的真实 api,如下所示:
通过这种方式,我们可以覆盖测试范围的类路径。我们可以在此处和此处。
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:
With this way, we override the classpath for our test scope. We can read about this maven behavior here and here.
通常,直接从 Oracle 下载 JavaEE JAR 就足够了:
http://www.mkyong.com/maven/how-to-download-j2ee-api-javaee-jar-from-maven/
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/