java.lang.NoClassDefFoundError: Lorg/apache/logging/log4j/Logger; but the artifact exists
Asked Answered
T

3

10

I'm using Tomcat to deploy a java webapp.

I get a very long stacktrace, in short:

GRAVE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/new-webapp]]
[...]
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/logging/log4j/Logger;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2509)
    at java.lang.Class.getDeclaredFields(Class.java:1819)
    at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
    at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:256)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationFilterAnnotations(WebAnnotationSet.java:105)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:64)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:335)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:782)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
    ... 20 more
[...]

Now, the error is pretty clear. For some reason, the log4j bundle is not in the classpath.

The application is a maven webapp, and the pom.xml is like this:

<project 
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wb</groupId>
    <artifactId>new-webapp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1</version>
    <properties>
        <log4j.version>2.5</log4j.version>
    </properties>

    <dependencies>
    [...]
        <!-- Logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    [...]
</project>

If I go to the project properties, under Libraries->Maven Dependencies I see this:

enter image description here

Libraries were found, downloaded and they are in the classpath.

I also tried to open a java class inside my project and declare

org.apache.logging.log4j.Logger Logger;

No errors, the Logger interface is found.

What's going on here? Why does Tomcat fail to start even if libraries are in the classpath?


Edit - this is the log4j configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>
Toft answered 22/9, 2016 at 8:20 Comment(11)
I can't see the image but this is Eclipse right? Are you deploying inside the IDE or in an external Tomcat? If inside IDE, make sure you clean both the server and the project and any temporary files, also make sure you use an up to date m2e and m2e-wtp. Are the libraries present inside WEB-INF/lib if you package the war with mvn clean package?Mccaffrey
@Mccaffrey I'm deploying inside eclipse, and I've already tried to clean both Tomcat, Project and Tomcat working directory. If I go under wtpwebapps in the eclipse server path, under WEB-INF/lib I can see three jog4j jars.Toft
please check this, I see there are lot more steps which need to be followed while using log4j with tomcat8 tomcat.apache.org/tomcat-8.0-doc/logging.htmlSakmar
@Sakmar But this is for configuring tomcat's internal logging. I don't want to do so, I just want to use log4j classes for logging in my application, tomcat can have its own logging libraries. And anyway, the issue is that log4j seems to be ignored by tomcat. The configuration is done inside the log4j file, so if log4j doesn't load it just won't work.Toft
Could you post the log4j configuration file?Binghi
@LittleSanti Sure, I edited the questionToft
No way. I don't even reproduce your error with the same versions and configuration :-( Have you got to deploy a HelloWorld webapp in that Tomcat instance?Binghi
@LittleSanti I managed to deploy this webapp without issues, but it doesn't use log4j :/Toft
@Toft did you find the solution ? am also facing the same issue.Thanks in advanceBottali
@Thanga Unfortunately no.Toft
@Toft and Thaga, did you find the solution ? I am also facing the same issue.Prussianism
B
11

By the description you've made, I assume you are working with Eclipse.

Well, you'd better go to Project properties -> Deployment assembly and ensure that the maven dependencies entry is included.

I've experimented often that this configuration gets missed whenever you execute Maven -> Update Project.

Binghi answered 22/9, 2016 at 8:27 Comment(3)
Yeah, it happened to me too before, but that's not the case, Maven Dependencies are there, with WEB-INF/lib as Deploy PathToft
@Toft And log4j is included in that Maven Dependencies entry, sure?Binghi
Yes, as the picture shows. Moreover, there are other configurations done before the log4j one, all coming from libraries loaded with maven, they don't give errors, log4j does. Pretty weird.Toft
T
4

I'm working with Eclipse and I had same problem every time I made changes in my pom.xml. I don't know why but Eclipse delete the Maven Dependencies.

Solution: rigth click on project, select Properties, choose Deployment Assembly and verify in column "Source" a row called "Maven Dependencies". If it isn't there, click on Add..., Java Build Path Entries and click on Maven Dependencies. Finally Apply and close.

Thriftless answered 1/8, 2018 at 7:53 Comment(1)
Read comments please - Moreover, there are other configurations done before the log4j one, all coming from libraries loaded with maven, they don't give errors, log4j does. Pretty weird - it means that maven dependencies are already in the classpath, otherwise all maven libraries would give errors.Toft
B
0

Ensure that the log4j libraries are not included already in the Tomcat runtime (nor the endorsed directories), because it could produce conflict about classloading policy.

Binghi answered 22/9, 2016 at 8:41 Comment(1)
Tomcat is a fresh install, version 8.0.37 downloaded yesterday from the Apache site. These are the libraries: i.sstatic.net/nr2F2.pngToft

© 2022 - 2024 — McMap. All rights reserved.