You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven+JavaFX项目创建JPA实体类后编译报错:java.lang.NoClassDefFoundError: javax.persistence.spi.PersistenceUnitInfo

Fixing javax.persistence.spi.PersistenceUnitInfo NoClassDefFoundError in JavaFX + JPA Maven Project

Hey there! Let's break down this compile error you're facing. The NoClassDefFoundError for javax.persistence.spi.PersistenceUnitInfo means your Maven build can't find the core JPA API classes during compilation—this usually happens when the JPA API dependency isn't properly included in your compile classpath, or there's a mismatch in dependency versions/scope.

Looking at your pom.xml and setup, here are the key fixes to resolve this:

1. Replace the JPA API Dependency with the Standard Artifact

Your current javax.persistence dependency uses EclipseLink's packaged version, which can cause classpath issues with Java 11 (since Java 11 removed bundled Java EE modules). Swap it out for the official JPA API artifact:

Remove this dependency:

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>javax.persistence</artifactId>
  <version>2.2.1</version>
</dependency>

Add this standard JPA API dependency instead:

<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>javax.persistence-api</artifactId>
  <version>2.2</version>
</dependency>

2. Update persistence.xml to Match JPA 2.2 Version

Your persistence.xml uses version 2.1, but we're using JPA 2.2 now. Update the root element to match:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
                                 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <!-- Your existing persistence-unit configuration stays the same -->
</persistence>

3. Clean and Rebuild Your Project

After making these changes, run the following Maven commands to ensure a fresh build:

mvn clean
mvn compile javafx:run

Why This Works

  • The standard javax.persistence-api artifact provides the core JPA interface classes (like PersistenceUnitInfo) that your entity classes and JPA setup need during compilation.
  • Aligning the persistence.xml version with your JPA API version ensures compatibility between your configuration and the API implementation.

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 15:17:48