java.lang.ClassNotFoundException:org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor 在 GlassFish 4.1 上部署 Java EE 7 应用程序时

java.lang.ClassNotFoundException: org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor when deploying Java EE 7 app on GlassFish 4.1

最近,在将我的应用程序部署到 GlassFish 4.1 测试服务器时,出现以下异常:

Exception while loading the app : java.lang.ClassNotFoundException: org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor

同一应用程序服务器版本上的本地部署按预期工作。我的本地环境和测试服务器都使用 java8 release 31.

异常的完整堆栈跟踪:

Exception while invoking class org.glassfish.ejb.startup.EjbApplication start method
java.lang.RuntimeException: java.lang.ClassNotFoundException: org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor
        at com.sun.ejb.containers.BaseContainer.setStartedState(BaseContainer.java:962)
        at org.glassfish.ejb.startup.EjbApplication.markAllContainersAsStarted(EjbApplication.java:140)
        at org.glassfish.ejb.startup.EjbApplication.start(EjbApplication.java:152)
        at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
        at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
        at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:500)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:539)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:535)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.execute(CommandRunnerImpl.java:534)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:565)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:557)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access00(CommandRunnerImpl.java:109)
       at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
        at org.glassfish.admin.rest.resources.admin.CommandResource.executeCommand(CommandResource.java:404)
        at org.glassfish.admin.rest.resources.admin.CommandResource.execCommandSimpInMultOut(CommandResource.java:234)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:151)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)   
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
        at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:271)
        at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
        at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:365)
        at org.glassfish.admin.rest.adapter.JerseyContainerCommandService.service(JerseyContainerCommandService.java:173)
        at org.glassfish.admin.rest.adapter.RestAdapter.service(RestAdapter.java:179)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:56)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor
        at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1783)
        at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1633)
        at com.sun.ejb.containers.interceptors.InterceptorManager.buildEjbInterceptorChain(InterceptorManager.java:431)
        at com.sun.ejb.containers.interceptors.InterceptorManager.<init>(InterceptorManager.java:131)
        at com.sun.ejb.containers.BaseContainer.initializeInterceptorManager(BaseContainer.java:3396)
        at com.sun.ejb.containers.BaseContainer.setStartedState(BaseContainer.java:950)
        ... 67 more
]]

[2015-02-26T11:05:11.836+0100] [glassfish 4.1] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=45 _ThreadName=admin-listener(5)] [timeMillis: 1424945111836] [levelValue: 1000] [[
  Exception during lifecycle processing   
java.lang.RuntimeException: java.lang.ClassNotFoundException: org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor
        at com.sun.ejb.containers.BaseContainer.setStartedState(BaseContainer.java:962)
        at org.glassfish.ejb.startup.EjbApplication.markAllContainersAsStarted(EjbApplication.java:140)
        at org.glassfish.ejb.startup.EjbApplication.start(EjbApplication.java:152)
        at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
        at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
        at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:500)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:539)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:535)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.execute(CommandRunnerImpl.java:534)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:565)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.run(CommandRunnerImpl.java:557)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access00(CommandRunnerImpl.java:109)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
        at org.glassfish.admin.rest.resources.admin.CommandResource.executeCommand(CommandResource.java:404)
        at org.glassfish.admin.rest.resources.admin.CommandResource.execCommandSimpInMultOut(CommandResource.java:234)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:151)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)   
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
        at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:271)
        at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
        at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:365)
        at org.glassfish.admin.rest.adapter.JerseyContainerCommandService.service(JerseyContainerCommandService.java:173)
        at org.glassfish.admin.rest.adapter.RestAdapter.service(RestAdapter.java:179)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:56)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor
        at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1783)
        at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1633)
        at com.sun.ejb.containers.interceptors.InterceptorManager.buildEjbInterceptorChain(InterceptorManager.java:431)
        at com.sun.ejb.containers.interceptors.InterceptorManager.<init>(InterceptorManager.java:131)
        at com.sun.ejb.containers.BaseContainer.initializeInterceptorManager(BaseContainer.java:3396)
        at com.sun.ejb.containers.BaseContainer.setStartedState(BaseContainer.java:950)
        ... 67 more

我通过在我的 WEB-INF 文件夹中定义一个 beans.xml 文件来明确启用 CDI:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="annotated">
</beans>

Jars 打包在 war:

    10133  2013-11-27 23:28   WEB-INF/lib/maven-plugin-api-2.0.jar
    33403  2014-10-14 13:44   WEB-INF/lib/casablanca-1.0.10.jar
   104615  2013-11-27 23:28   WEB-INF/lib/maven-project-2.0.jar
    78761  2013-11-27 23:28   WEB-INF/lib/maven-artifact-2.0.jar
   300845  2014-10-29 11:31   WEB-INF/lib/jsoup-1.8.1.jar
    51560  2013-11-27 23:28   WEB-INF/lib/wagon-provider-api-1.0-alpha-5.jar
   591683  2015-01-27 11:40   WEB-INF/lib/omnifaces-2.0.jar
   160291  2013-11-20 13:15   WEB-INF/lib/hibernate-jpamodelgen-1.1.1.Final.jar
   100884  2013-11-20 13:15   WEB-INF/lib/hibernate-jpa-2.0-api-1.0.0.Final.jar
    66440  2015-02-17 11:13   WEB-INF/lib/liquibase-maven-plugin-3.1.1.jar
    30408  2013-11-27 23:28   WEB-INF/lib/maven-profile-2.0.jar
    50628  2013-11-27 23:28   WEB-INF/lib/maven-artifact-manager-2.0.jar
   195436  2013-11-27 23:29   WEB-INF/lib/plexus-container-default-1.0-alpha-8.jar
  2693611  2014-10-21 09:09   WEB-INF/lib/primefaces-5.1.jar
   273449  2013-11-29 12:44   WEB-INF/lib/snakeyaml-1.13.jar
  1265562  2014-12-17 12:15   WEB-INF/lib/liquibase-core-3.1.1.jar
    78476  2013-11-27 23:28   WEB-INF/lib/maven-model-2.0.jar
   480632  2015-02-05 12:13   WEB-INF/lib/facebook4j-core-2.2.2.jar
    20655  2013-11-27 23:28   WEB-INF/lib/maven-repository-metadata-2.0.jar
    37516  2013-11-27 23:28   WEB-INF/lib/classworlds-1.1-alpha-2.jar
   163803  2013-05-26 19:39   WEB-INF/lib/plexus-utils-1.0.4.jar

我尝试在本地复制整个测试服务器,我的 war 成功部署在上面。我也尝试在服务器上复制我本地的glassfish,然后部署失败。

使用 -verbose:class jvm 选项,我注意到有问题的 class 从 glassfish/modules/bean-validator-cdi.jar 包中加载了一次,在工作的本地服务器和失败的服务器上远程服务器。这个包两边一模一样

我现在正在尝试减少正确部署的最后一个 jar 和当前版本之间的代码差异。在这些版本之间,与 pom 的差异仅包括已删除的 commons- 库。与 web.xml 的差异仅包括已删除的 Primefaces fileuplaod 过滤器。

感谢任何建议。

似乎有些微妙的 class 加载差异。找到差异的一个相对简单的方法是 运行 工作和非工作服务器

java -verbose:class

并比较输出。如果服务器相同,则差异应该相对较小。我发现这个冗长的选项在过去非常有用。

我设法找到了这个问题的原因。

编辑: 这是由这个错误引起的:https://github.com/javaee/hk2/issues/277

将 glassfish 模块目录中的 bean-validator.jar 和 bean-validator-cdi.jar 替换为 2.4-b06 版本(org.glassfish.hk2.external 组)解决了问题。


原文post:

它是 @Stateless bean 中方法参数的 @javax.validation.constraint.NotNull 注释。 我不知道它是如何导致部署错误的,但它解决了这个问题。