Uploaded image for project: 'TomEE'
  1. TomEE
  2. TOMEE-4390

ClassNotFoundException in webapp that has log4j2 and invokes commons-logging

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 10.0.0-M2
    • 10.0.0-M3
    • TomEE Core Server
    • None

    Description

      Accessing commons-logging throws an exception when it is being invoked in an application that has a log4j2 dependency but no commons-logging dependency. Might possibly also be a commons-logging bug? Really not sure

      The layout is basically:

      • tomee
        • libs/
          • commons-logging.jar
        • webapps/ROOT/WEB-INF/libs
          • log4j2-api/core.jar

       

      This causes the following to happen:

      1. commons-logging checks the current thread classloader for a log4j2 class
      2. if it finds one, it creates an instance of Log4jApiLogFactory
      3. while doing so, java tries to load the class org.apache.logging.log4j.spi.LoggerAdapter using the classloader that loaded commons-logging.jar
      4. ClassNotFoundException with a very incomprehensible stacktrace

       

      See attached reproducer, simply run mvn package tomee:run

       

      Stacktrace:

              Caused by: java.lang.NoClassDefFoundError: org/apache/logging/log4j/spi/LoggerAdapter
                      at java.base/java.lang.Class.forName0(Native Method)
                      at java.base/java.lang.Class.forName(Class.java:375)
                      at org.apache.commons.logging.LogFactory.createFactory(LogFactory.java:419)
                      at org.apache.commons.logging.LogFactory.lambda$newFactory$3(LogFactory.java:1431)
                      at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
                      at org.apache.commons.logging.LogFactory.newFactory(LogFactory.java:1431)
                      at org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:928)
                      at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:987)
                      at com.example.ExampleBean.initApplication(ExampleBean.java:15)
                      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
                      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                      at java.base/java.lang.reflect.Method.invoke(Method.java:569)
                      at org.apache.webbeans.event.ObserverMethodImpl.invoke(ObserverMethodImpl.java:410)
                      at org.apache.webbeans.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:371)
                      ... 49 more
              Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.spi.LoggerAdapter
                      at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
                      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:592)
                      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
                      ... 64 more
      
      

       

      Attachments

        1. log4j2-commons-logging.zip
          2 kB
          Markus Jung

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jungm Markus Jung
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: