Solr
  1. Solr
  2. SOLR-5363

NoClassDefFoundError when using Apache Log4J2

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 4.5
    • Fix Version/s: 4.6, Trunk
    • Component/s: None
    • Labels:

      Description

      Hey guys,

      I'm using Log4J2 + SLF4J in my project. Unfortunately my embedded solr server throws this error when starting:

      Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.da
      ta.solr.core.SolrOperations com.xxxxx.platform.core.config.SolrsearchConfig.defaultSolrTemplate() throws javax.xml.par
      sers.ParserConfigurationException,java.io.IOException,org.xml.sax.SAXException] threw exception; nested exception is org
      .springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultSolrServer' defined in class
       path resource [com/xxxxx/platform/core/config/SolrsearchConfig.class]: Instantiation of bean failed; nested exception
       is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.solr.client.solrj.
      SolrServer com.xxxxxx.platform.core.config.SolrsearchConfig.defaultSolrServer() throws javax.xml.parsers.ParserConfigur
      ationException,java.io.IOException,org.xml.sax.SAXException] threw exception; nested exception is java.lang.NoClassDefFo
      undError: org/apache/log4j/Priority
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy
      .java:181)
              at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolv
      er.java:570)
              ... 105 more
      Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultSolrServer' de
      fined in class path resource [com/xxxxxx/platform/core/config/SolrsearchConfig.class]: Instantiation of bean failed; ne
      sted exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.solr
      .client.solrj.SolrServer com.xxxxxxx.platform.core.config.SolrsearchConfig.defaultSolrServer() throws javax.xml.parsers.
      ParserConfigurationException,java.io.IOException,org.xml.sax.SAXException] threw exception; nested exception is java.lan
      g.NoClassDefFoundError: org/apache/log4j/Priority
              at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolv
      er.java:581)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(Ab
      stractAutowireCapableBeanFactory.java:1025)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutow
      ireCapableBeanFactory.java:921)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCap
      ableBeanFactory.java:487)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapab
      leBeanFactory.java:458)
              at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
              at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegis
      try.java:223)
              at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
              at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(Configurati
      onClassEnhancer.java:298)
              at com.xxxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6.defaultSolrServer(<generated>)
              at com.xxxxx.platform.core.config.SolrsearchConfig.defaultSolrTemplate(SolrsearchConfig.java:37)
              at com.xxxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6.CGLIB$defaultSolrTemplate$2(<gen
      erated>)
              at com.xxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6$$FastClassByCGLIB$$f67069c2.invo
      ke(<generated>)
              at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
              at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(Configurati
      onClassEnhancer.java:286)
              at com.xxxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6.defaultSolrTemplate(<generated>)
      
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy
      .java:160)
              ... 106 more
      Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.solr.client
      .solrj.SolrServer com.xxxxxxx.platform.core.config.SolrsearchConfig.defaultSolrServer() throws javax.xml.parsers.ParserC
      onfigurationException,java.io.IOException,org.xml.sax.SAXException] threw exception; nested exception is java.lang.NoCla
      ssDefFoundError: org/apache/log4j/Priority
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy
      .java:181)
              at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolv
      er.java:570)
              ... 127 more
      Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Priority
              at org.apache.solr.core.CoreContainer.load(CoreContainer.java:433)
              at org.apache.solr.core.CoreContainer.load(CoreContainer.java:358)
              at org.apache.solr.core.CoreContainer.<init>(CoreContainer.java:160)
              at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.createPathConfiguredSolrServer(Embedde
      dSolrServerFactory.java:101)
              at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.initSolrServer(EmbeddedSolrServerFacto
      ry.java:77)
              at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.getSolrServer(EmbeddedSolrServerFactor
      y.java:70)
              at com.xxxxxxx.platform.core.config.SolrsearchConfig.defaultSolrServer(SolrsearchConfig.java:32)
              at com.xxxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6.CGLIB$defaultSolrServer$1(<gener
      ated>)
              at com.xxxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6$$FastClassByCGLIB$$f67069c2.invo
      ke(<generated>)
              at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
              at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(Configurati
      onClassEnhancer.java:286)
              at com.xxxxxx.platform.core.config.SolrsearchConfig$$EnhancerByCGLIB$$c571c5a6.defaultSolrServer(<generated>)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy
      .java:160)
              ... 128 more
      Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
              at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
              at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
              at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
              at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:430)
              at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:383)
              ... 145 more
      

      Seems like Solr relies on Log4J version 1.x.

      1. SOLR-5363.patch
        0.9 kB
        Petar Tahchiev

        Activity

        Hide
        Steve Rowe added a comment -

        Hi Petar, this missed the 4.5.1 cutoff, I'm afraid. It will be in 4.6.

        Alan, can you set the fix version to 4.6 and 5.0? I do this when I resolve issues.

        Show
        Steve Rowe added a comment - Hi Petar, this missed the 4.5.1 cutoff, I'm afraid. It will be in 4.6. Alan, can you set the fix version to 4.6 and 5.0? I do this when I resolve issues.
        Hide
        Alan Woodward added a comment -

        Hi Petar, this missed the 4.5.1 cutoff, I'm afraid. It will be in 4.6.

        Show
        Alan Woodward added a comment - Hi Petar, this missed the 4.5.1 cutoff, I'm afraid. It will be in 4.6.
        Hide
        Petar Tahchiev added a comment -

        Hi Alan,

        I just used Solr 4.5.1 from m2 official repo and it seems like it is still not fixed:

                if (slf4jImpl.indexOf("Log4j") > 0) {
                  fname = "Log4j";
                } else if (slf4jImpl.indexOf("JDK") > 0) {
                  fname = "JUL";
                }
        

        This is inside the jar. Should I reopen the issue?

        Show
        Petar Tahchiev added a comment - Hi Alan, I just used Solr 4.5.1 from m2 official repo and it seems like it is still not fixed: if (slf4jImpl.indexOf( "Log4j" ) > 0) { fname = "Log4j" ; } else if (slf4jImpl.indexOf( "JDK" ) > 0) { fname = "JUL" ; } This is inside the jar. Should I reopen the issue?
        Hide
        Alan Woodward added a comment -

        Thanks Petar!

        Show
        Alan Woodward added a comment - Thanks Petar!
        Hide
        ASF subversion and git services added a comment -

        Commit 1535066 from Alan Woodward in branch 'dev/branches/branch_4x'
        [ https://svn.apache.org/r1535066 ]

        SOLR-5363: Solr doesn't start up properly with Log4J2

        Show
        ASF subversion and git services added a comment - Commit 1535066 from Alan Woodward in branch 'dev/branches/branch_4x' [ https://svn.apache.org/r1535066 ] SOLR-5363 : Solr doesn't start up properly with Log4J2
        Hide
        ASF subversion and git services added a comment -

        Commit 1535065 from Alan Woodward in branch 'dev/trunk'
        [ https://svn.apache.org/r1535065 ]

        SOLR-5363: Solr doesn't start up properly with Log4J2

        Show
        ASF subversion and git services added a comment - Commit 1535065 from Alan Woodward in branch 'dev/trunk' [ https://svn.apache.org/r1535065 ] SOLR-5363 : Solr doesn't start up properly with Log4J2
        Hide
        Alan Woodward added a comment -

        Hi Petar, thanks for this. It looks as though your patch is against a slightly older version of Solr (the relevant code has moved out of CoreContainer and into LogWatcher now), but it's simple to apply it.

        Show
        Alan Woodward added a comment - Hi Petar, thanks for this. It looks as though your patch is against a slightly older version of Solr (the relevant code has moved out of CoreContainer and into LogWatcher now), but it's simple to apply it.
        Hide
        Shikhar Bhushan added a comment - - edited

        Confirming the issue & Petar's assessment, ran into this as well

        Show
        Shikhar Bhushan added a comment - - edited Confirming the issue & Petar's assessment, ran into this as well
        Hide
        Petar Tahchiev added a comment -

        Hi guys,

        so It seems that the CoreContainer checks if the slf4j implementation name contains log4j and if so instantiates the Log4j watcher. The problem is that using the Log4J2 the slf4j implementation is:

        org.slf4j.helpers.Log4jLoggerFactory
        

        so you see it does contain "Log4J", and thus SOLR will try to instantiate the Log4JWatcher, which was written for Log4J1. The Log4J1 slf4j implementation is called:

        org.slf4j.impl.Log4jLoggerFactory
        

        so what I have done is to change the if to include the full class name. If you apply this patch i guess the issue can be closed, but further on it might be a good idea to implement a Log4J2Watcher.

        Show
        Petar Tahchiev added a comment - Hi guys, so It seems that the CoreContainer checks if the slf4j implementation name contains log4j and if so instantiates the Log4j watcher. The problem is that using the Log4J2 the slf4j implementation is: org.slf4j.helpers.Log4jLoggerFactory so you see it does contain "Log4J", and thus SOLR will try to instantiate the Log4JWatcher, which was written for Log4J1. The Log4J1 slf4j implementation is called: org.slf4j.impl.Log4jLoggerFactory so what I have done is to change the if to include the full class name. If you apply this patch i guess the issue can be closed, but further on it might be a good idea to implement a Log4J2Watcher.

          People

          • Assignee:
            Alan Woodward
            Reporter:
            Petar Tahchiev
          • Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development