java - java.lang.ClassNotFoundException: javax.persistence.Persistence - 最少的工作,基于 maven,需要示例
问题描述
我正在尝试使用 EclipseLink + JPA 开始一个新的 Web 项目。在开始执行之前一切正常 - 一旦建立到数据库的连接,就会引发异常:
“java.lang.ClassNotFoundException:javax.persistence.Persistence”。
我想我一般都知道这个错误是什么意思(java 找不到类);但是,我无法克服它。我尝试了 StackOverflow 中的许多示例,显示应该导入哪些依赖项,但没有一个对我有用。
我创建了一个最小的不起作用的例子。
简单的 JSP 页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%
Class.forName("javax.persistence.Persistence");
%>
</body>
</html>
和具有依赖关系的 Maven
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.15.2.0</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/org.eclipse.persistence.jpa -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.7</version>
<scope>runtime</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.eclipse.persistence</groupId>-->
<!-- <artifactId>eclipselink</artifactId>-->
<!-- <version>2.5.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.persistence</groupId>-->
<!-- <artifactId>javax.persistence</artifactId>-->
<!-- <version>2.0.0</version>-->
<!-- <scope>compile</scope>-->
<!-- </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.2.Final</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<!-- <dependency>-->
<!-- <groupId>org.hibernate.javax.persistence</groupId>-->
<!-- <artifactId>hibernate-jpa-2.0-api</artifactId>-->
<!-- <version>1.0.1.Final</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.persistence</groupId>-->
<!-- <artifactId>eclipselink</artifactId>-->
<!-- <version>2.5.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.persistence</groupId>-->
<!-- <artifactId>javax.persistence</artifactId>-->
<!-- <version>2.1.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.hibernate.javax.persistence</groupId>-->
<!-- <artifactId>hibernate-jpa-2.1-api</artifactId>-->
<!-- <version>1.0.0.Final</version>-->
<!-- </dependency>-->
</dependencies>
- 我已经尝试了所有这些,包括评论。环境:IntelliJ Idea,Java 13。
可能有其他问题,还是我忽略了什么?
如果有人可以向我发送pom.xml
正在工作的文件,将不胜感激。
提前谢谢你的帮助。
解决方案
好的,终于明白了,也许对某人有帮助。
主要问题是该库虽然由 Maven 正确导入,但并未在工件中导出。因此,执行的 Web 应用程序无法找到该类。
下载的 .jar 文件必须添加到输出工件的“lib”文件夹中。
可能对某人有帮助。
推荐阅读
- http - 如何在 Go 中使用不记名令牌发出请求
- python - apa102 LED 亮度控制
- python - python中的终端命令不起作用
- python - 操作类实例
- amazon-web-services - Lambda + API Gateway /**/* 路由?
- ruby-on-rails - Capybara:点击里面的文字
- javascript - 如何在其他功能运行时禁用 JavaScript 功能
- html - Vuetify - 将 TR 高度设置为低于最小值
- css - 悬停动画不惊人
- bash - 如何在 Unix/Linux 控制台中使用 firefox 从文件中打开 URL 地址?