什么可能导致我的NoClassDefFoundError?

时间:2017-05-25 19:39:38

标签: java tomcat7 drools kie

我有一个现有的Web服务项目,我正在努力将Drools附加到处理这些服务提交的业务规则。

在编译时单元测试中似乎一切正常,但是当我在测试服务器上的Tomcat 7容器中运行项目时,我得到以下堆栈跟踪:

05/25/2017 13:38:53.052 (EDT):[com.myproject.helpers.BusinessRuleHelper.checkBusinessRules]:[SEVERE]- Failed to process our request
05/25/2017 13:38:53.052 (EDT):[com.myproject.helpers.BusinessRuleHelper.checkBusinessRules]:[SEVERE]- Could not initialize class org.drools.compiler.kie.builder.impl.KieContainerImpl
05/25/2017 13:38:53.052 (EDT):[com.myproject.helpers.BusinessRuleHelper.checkBusinessRules]:[SEVERE]- class java.lang.NoClassDefFoundError
05/25/2017 13:38:53.053 (EDT):[com.myproject.helpers.BusinessRuleHelper.checkBusinessRules]:[SEVERE]- java.lang.NoClassDefFoundError: Could not initialize class org.drools.compiler.kie.builder.impl.KieContainerImpl
    at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieClasspathContainer(KieServicesImpl.java:135)
    at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:101)
    at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:79)
    at com.myproject.rules.Engine.processRequest(Engine.java:19)
    at com.myproject.helpers.BusinessRuleHelper.checkBusinessRules(BusinessRuleHelper.java:366)
    at com.myproject.helpers.BusinessRuleHelper.checkCriticalErrorBusinessRules(BusinessRuleHelper.java:291)
    at com.myproject.helpers.WebServiceHelper.processWebServiceRequest(WebServiceHelper.java:184)
    at com.myproject.webservicepackage.WebServiceImpl.webService(WebServiceImpl.java:40)
    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.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:172)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:88)
    at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:63)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:74)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:123)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:206)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:213)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:193)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:127)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:187)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:110)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:166)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:193)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我控制下的最后一个线框com.myproject.rules.Engine.processRequest

/**
 * Runs a Request through the business rule engine
 * @param request The input to validate
 */
public static KieSession processRequest(Request request) {
    // load up the knowledge base
    KieServices ks = KieServices.Factory.get();
    KieContainer kContainer = ks.getKieClasspathContainer(); // <- Error happens here
    // This name matches the ksessionname of the rules knowledge base in the kmodule.xml file
    String kSessionName = "ksession-rules";
    KieSession kSession = kContainer.newKieSession(kSessionName);

    // ...process the rules...
}

我很困惑,因为com.myproject.webservicepackage WAR文件以WEB-INF\lib\的方式包含Drools JAR,就像你期望的那样,最后几帧与Java所在的类在同一个库中找到。的定义。

我在库文件夹中看到的Drools JAR是:

  • 滴料编译-6.5.0.Final.jar
  • 滴料芯-6.5.0.Final.jar
  • 纪伊-API-6.5.0.Final.jar
  • 纪伊内部-6.5.0.Final.jar

据我所知,这些之间没有重叠,第一个是有问题的图书馆。

我继续调试这个,看起来这只是第二次和后续请求遇到的错误。第一个请求返回了这个堆栈跟踪:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type LoggerFactory; used in the signature
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:299)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.<clinit>(KieContainerImpl.java:92)
    at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieClasspathContainer(KieServicesImpl.java:135)
    at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:101)
    at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:79)
    at com.myproject.helpers.rules.Engine.processRequest(Engine.java:19)
    at com.myproject.helpers.BusinessRuleHelper.checkBusinessRules(BusinessRuleHelper.java:366)
    at com.myproject.helpers.BusinessRuleHelper.checkCriticalErrorBusinessRules(BusinessRuleHelper.java:291)
    at com.myproject.helpers.WebServiceHelper.processWebServiceRequest(WebServiceHelper.java:184)
    at com.myproject.webservicepackage.WebServiceImpl.webService(WebServiceImpl.java:40)
    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.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:172)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:88)
    ... 37 more

看起来它正在缓存此失败,因为没有类定义。

1 个答案:

答案 0 :(得分:0)

此消息:

  

java.lang.LinkageError:加载器约束违规:当解析方法“org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;”时,当前类的org/slf4j/LoggerFactory类加载器和已解析类的类加载器org/slf4j/impl/StaticLoggerBinder ,为LoggerFactory类型提供不同的Class对象;用于签名

...表示我的项目中SLF4J版本之间存在冲突。该库由我正在使用的一些依赖项提供,也由Tomcat容器本身提供。我使用Maven通过运行来找到使用SLF4J的工件:

mvn dependency:tree -Dverbose -Dincludes=org.slf4j

这让我得到了一个依赖树,它显示了SLF4J的使用位置:

[INFO] ------------------------------------------------------------------------
[INFO] Building business-rules 3.4-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ business-rules ---
[INFO] com.myproject:business-rules:jar:3.4-SNAPSHOT
[INFO] +- org.kie:kie-api:jar:6.5.0.Final:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.2:compile
[INFO] +- org.drools:drools-core:jar:6.5.0.Final:compile
[INFO] |  +- org.kie:kie-internal:jar:6.5.0.Final:compile
[INFO] |  |  \- (org.slf4j:slf4j-api:jar:1.7.2:compile - omitted for duplicate)
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.2:compile - omitted for duplicate)
[INFO] \- org.drools:drools-compiler:jar:6.5.0.Final:compile
[INFO]    \- (org.slf4j:slf4j-api:jar:1.7.2:compile - omitted for duplicate)

我能够通过修改项目的Maven POM来删除此处带来的版本,以便在这三个依赖项中的每一个中排除SLF4J:

<dependencies>
  ...
  <dependency>
      <groupId>org.kie</groupId>
      <artifactId>kie-api</artifactId>
      <version>${drools.version}</version>
      <exclusions> 
        <exclusion> 
          <groupId>org.slf4j</groupId> 
          <artifactId>slf4j-api</artifactId> 
        </exclusion> 
    </exclusions> 
  </dependency>
  <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-core</artifactId>
      <version>${drools.version}</version>
      <exclusions> 
        <exclusion> 
          <groupId>org.slf4j</groupId> 
          <artifactId>slf4j-api</artifactId> 
        </exclusion> 
    </exclusions> 
  </dependency>
  <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-compiler</artifactId>
      <version>${drools.version}</version>
      <exclusions> 
        <exclusion> 
          <groupId>org.slf4j</groupId> 
          <artifactId>slf4j-api</artifactId> 
        </exclusion> 
    </exclusions> 
  </dependency>
  ...
</dependencies>

然后为了让它在本地构建和测试,我仍然必须在同一个项目中添加SLF4J的显式编译时依赖项,以及导入它的那个,让Maven知道Tomcat容器将在运行时提供依赖:

<dependencies>
  ...
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.2</version>
    <scope>provided</scope>
  </dependency>
  ...
</dependencies>

这些变化似乎解决了我的问题。