Maven+JavaFX项目创建JPA实体类后编译报错:java.lang.NoClassDefFoundError: javax.persistence.spi.PersistenceUnitInfo
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-apiartifact provides the core JPA interface classes (likePersistenceUnitInfo) that your entity classes and JPA setup need during compilation. - Aligning the
persistence.xmlversion with your JPA API version ensures compatibility between your configuration and the API implementation.
内容的提问来源于stack exchange,提问作者Max

