javax.faces.el.E​​valuationException:java.lang.NoClassDefFoundError:javax / persistence / Persistence

时间:2012-10-11 16:48:09

标签: jsf jpa persistence

对于我正在研究的项目,我在Eclipse上使用JPA和JSF。最初我开发了数据层(数据库中的访问),它工作正常。但是当我将它与JSF项目集成并尝试通过bean连接到db时,我得到以下异常:

com.sun.faces.application.ActionListenerImpl processAction 严重:java.lang.NoClassDefFoundError:javax / persistence / Persistence

具体来说,它在尝试获取实体管理器工厂时崩溃了

EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

我已经包含了所有必需的库,包括javax / persistence。 我不明白为什么我会收到这个错误。 您可以在下面看到完整的异常堆栈以及我包含的库的映像。

有人可以告诉我我做错了什么吗?我也看过其他类似的问题,但无法解决我的问题。 谢谢你们

 SEVERE: java.lang.NoClassDefFoundError: javax/persistence/Persistence
javax.faces.el.EvaluationException: java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
    at javax.faces.component.UICommand.broadcast(UICommand.java:311)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at org.ResearchManagement.Handlers.UserHandler.getAllUsers(UserHandler.java:31)
    at org.tutorial.LoginBean.authenticate(LoginBean.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
    ... 24 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 34 more
11 Οκτ 2012 6:55:10 μμ com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{loginBean.authenticate}: java.lang.NoClassDefFoundError: javax/persistence/Persistence
javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
    at javax.faces.component.UICommand.broadcast(UICommand.java:311)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
    ... 23 more
Caused by: java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at org.ResearchManagement.Handlers.UserHandler.getAllUsers(UserHandler.java:31)
    at org.tutorial.LoginBean.authenticate(LoginBean.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
    ... 24 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 34 more

enter image description here

2 个答案:

答案 0 :(得分:1)

需要将这些JAR文件放在/WEB-INF/lib文件夹中,而不是手动添加到项目的 Build Path 属性中。

撤消您在项目的 Build Path 属性中所做的每一项更改(重新创建项目可能更容易)并放置您的webapp所依赖的JAR文件(并且尚未提供由容器本身直接在/WEB-INF/lib文件夹中,而不是摆弄/修改任何IDE项目设置。

答案 1 :(得分:1)

NoClassDefFoundError是由于在运行时缺少类定义

我有一个非常类似的问题,但我使用的是Jdeveloper。 当我添加一个外部库时,我忘了检查选项"默认部署"所以我相信在运行之前可能不会部署添加的jar文件。

我不知道是否有一个选项"默认部署"在日食中,我建议你在添加图书馆时寻找类似的东西