我已经在Stackoverflow上搜索了这个问题的答案,但我能找到的唯一答案(here)不起作用(我添加了hibernate-jpa并没有区别)。
以下是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 / main / resources / 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文件时,我得到以下stacktrace。
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,而且我确实将Java设置为使用SE-1.8。
谢谢。
答案 0 :(得分:0)
尝试将此添加到您的pom.xml:
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
答案 1 :(得分:0)
这个问题的答案在于Maven POM文件。
需要Maven shade插件,然后将所有必需的jar文件捆绑/打包到最终的目标jar文件中。这消除了NoClassDefFoundError。这个post就是这个答案的参考。
接下来,persistence.xml文件被明确列为POM文件中的资源。这个post提供了参考。
这是更新的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.4.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</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>
<mainClass>ca.thirdgear.healthmonitor.SystemMonitor</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>persistence.xml</include>
</includes>
</resource>
</resources>
</build>
</project>