首页 > 解决方案 > 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>-->

<!--        &lt;!&ndash; https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api &ndash;&gt;-->
<!--        <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>

可能有其他问题,还是我忽略了什么?

如果有人可以向我发送pom.xml正在工作的文件,将不胜感激。

提前谢谢你的帮助。

标签: javamavenjpa

解决方案


好的,终于明白了,也许对某人有帮助。

主要问题是该库虽然由 Maven 正确导入,但并未在工件中导出。因此,执行的 Web 应用程序无法找到该类。

下载的 .jar 文件必须添加到输出工件的“lib”文件夹中。

可能对某人有帮助。


推荐阅读