我已經搜索了Stackoverflow爲這個問題的答案,但唯一的答案我可以找到(here)不起作用(我添加了hibernate-jpa這沒有什麼區別)。爲什麼我得到這個錯誤?異常在線程「main」java.lang.NoClassDefFoundError:javax/persistence/Persistence
以下是GitHub上關於此project的源代碼的鏈接。以下是有關的主要文件。
POM文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ca.thirdgear</groupId>
<artifactId>healthmonitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>healthmonitor</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.4.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<archive>
<manifest>
<mainClass>ca.thirdgear.healthmonitor.SystemMonitor</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ca.thirdgear.healthmonitor.SystemMonitor</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
</resource>
</resources>
</build>
</project>
的persistence.xml @的src /主/資源/ META-INF:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1.xsd"
version="2.1">
<persistence-unit name="healthmonitor" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ca.thirdgear.healthcheck.entities.User</class>
<class>ca.thirdgear.healthcheck.entities.TwilioAccount</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.1.172:3306/HomeAutomation" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="xxxxxxxxxxxxxxxx" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
主類:
package ca.thirdgear.healthmonitor;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import entities.User;
public class SystemMonitor
{
public static void main(String[] args)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("healthmonitor");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List<User> users = em.createNamedQuery("User.findAll", User.class).getResultList();
for(User u : users)
{
System.out.println(u.getFirstName());
}
em.getTransaction().commit();
em.close();
emf.close();
}
}
用戶實體類:
package entities;
import java.security.Principal;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@Table(name = "AuthenticatedUsers")
@NamedQueries(
{
@NamedQuery(name = "User.findByEmail",
query = "SELECT u FROM User u WHERE u.email = :email"),
@NamedQuery(name = "User.findByEmailAndPassword",
query = "SELECT u FROM User u WHERE u.email = :email AND u.password = :password"),
@NamedQuery(name = "User.findByPassword",
query = "SELECT u FROM User u WHERE u.password = :password"),
@NamedQuery(name = "User.findAll",
query = "SELECT a FROM User a")
}
)
/**
* Defines an Authenticated application User.
*/
public class User implements Principal
{
//PK for entity
@Id
@Basic(optional = false)
@NotNull
@Column(name = "email", nullable = false)
private String email;
@Basic(optional = false)
@NotNull
@Column(name = "firstName", nullable = false)
private String firstName;
@Basic(optional = false)
@NotNull
@Column(name = "lastName", nullable = false)
private String lastName;
@Basic(optional = false)
@NotNull
@Column(name = "phoneNumber", nullable = false)
private String phoneNumber;
@Basic(optional = false)
@NotNull
@Column(name = "role", nullable = false)
private String role;
@Basic(optional = false)
@NotNull
@Column(name = "password", nullable = false)
private String password;
@Basic(optional = false)
@NotNull
@Column(name = "activeStatus", nullable = false, length = 1)
private String activeStatus;
//default constructor
public User()
{
//not used
}
//constructor
public User(String firstName, String password, String role)
{
this.firstName = firstName;
this.password = password;
this.role = role;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber)
{
this.phoneNumber = phoneNumber;
}
public String getRole()
{
return role;
}
public void setRole(String role)
{
this.role = role;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getActiveStatus()
{
return activeStatus;
}
public void setActiveStatus(String activeStatus)
{
this.activeStatus = activeStatus;
}
@Override
public int hashCode()
{
return Objects.hash(this.firstName, this.lastName, this.phoneNumber,
this.email, this.role, this.password, this.activeStatus);
}
@Override
public String toString()
{
return "User{" + ", firstName=" + firstName
+ ", lastName=" + lastName
+ ", phoneNumber=" + phoneNumber
+ ", email=" + email
+ ", role=" + role
+ ", password=" + password
+ ", activeStatus=" + activeStatus;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final User other = (User) obj;
return Objects.equals(this.firstName, other.firstName)
&& Objects.equals(this.lastName, other.lastName)
&& Objects.equals(this.phoneNumber, other.phoneNumber)
&& Objects.equals(this.email, other.email)
&& Objects.equals(this.role, other.role)
&& Objects.equals(this.password, other.password)
&& Objects.equals(this.activeStatus, other.activeStatus);
}
/**
* Method implementation from Principal interface.
*
* @return The name of the Principal.
*/
public String getName() {
return firstName;
}
}
在這一點上,我所關心的只是從一個實體中讀取一些屬性並將其打印到命令行中。當我執行我的jar文件時,我得到以下堆棧跟蹤。
MikesMBP:target Mike$ java -jar healthmonitor-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence
at ca.thirdgear.healthmonitor.SystemMonitor.main(SystemMonitor.java:41)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我的persistence.xml文件不正確嗎?它是我的POM文件嗎?有一些配置我錯過了嗎?我真的很難過,因爲我認爲這將是一項微不足道的任務。
如果有問題我使用Eclipse Neon,並且我確實已經設置了使用SE-1.8的Java。
謝謝。
您確定它*是*使用1.8嗎? – nitind