java.lang.NoClassDefFoundError: javax/persistence/Persistence

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2850850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:38:37  来源:igfitidea点击:

java.lang.NoClassDefFoundError: javax/persistence/Persistence

javapersistence

提问by mehdi shahdoost

i'm using Netbeans 6.8 and build simple Maven web application project.

我正在使用 Netbeans 6.8 并构建简单的 Maven Web 应用程序项目。

create Entity and main file for persist Entity [also create persist unit] and use EclipsLink.

为持久实体创建实体和主文件 [也创建持久单元] 并使用 EclipsLink。

but when i run main file get this error :

但是当我运行主文件时出现此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence
        at Main.main(Main.java:34)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
        at java.net.URLClassLoader.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 1 more

in netbeans projects window and in my project libraries folder only add this package :

在 netbeans 项目窗口和我的项目库文件夹中只添加这个包:

javaee-web-api-6.0.jar [provided]

javaee-web-api-6.0.jar [提供]

please help me for solve this Error

请帮我解决这个错误

thanks

谢谢

回答by Binil Thomas

In your pom.xmlyou need to add a dependency to JPA libraries.

在您pom.xml需要向 JPA 库添加依赖项。

回答by Hunter Zhao

Click Hereto download hibernate-jpa-2.0-api-1.0.1.Final.jarand put it into the project library, your application will work fine. Good luck:)

点击这里下载hibernate-jpa-2.0-api-1.0.1.Final.jar并放入项目库中,您的应用程序将正常运行。祝你好运:)

回答by Bwire

Just incase you stumble upon this. Another problem could be you have a project importing another project the parent project is the one who is supposed to hold all the libraries.

以防万一你偶然发现了这一点。另一个问题可能是您有一个项目正在导入另一个项目,而父项目是应该保存所有库的项目。

回答by Francisco Fontes

I had the same problem and I solved it by changing the dependenciesto the following :

我遇到了同样的问题,我通过将依赖项更改为以下内容来解决它:

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

回答by Amit Mishra

for maven quickstart artifacts:-

对于 Maven 快速入门工件:-

This problems generally occures when there is no hibernate-jpa-2.0-api jar in classpath so first add this jar in your classpath. if still you are getting excption then go to C:\Users\(system user name)\.m2\repository\org\hibernate; delete folder javax. after that again download jars from pom.xml it will work.

当类路径中没有 hibernate-jpa-2.0-api jar 时,通常会出现此问题,因此请先在类路径中添加此 jar。如果仍然出现异常,请转到 C:\Users\(system user name)\.m2\repository\org\hibernate; 删除文件夹 javax. 之后再次从 pom.xml 下载 jars 它将工作。

for webapp:--

对于网络应用程序:--

first make sure hibernate-jpa-2.0-api jar downloaded and it is available in your build path. after that right click on project-->Build Path--->configure build path--->deployment Assembly--->Add--->java build path entries--->select the available jar(hibernate-jpa-2.0-api)--->next--->finish.

首先确保下载了 hibernate-jpa-2.0-api jar 并且它在您的构建路径中可用。之后右键单击项目-->构建路径--->配置构建路径--->部署程序集--->添加--->java构建路径条目--->选择可用的jar(hibernate-jpa- 2.0-api)--->下一步--->完成。