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>
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 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`? – Tunaki Sep 22 '16 at 08:33
  • @Tunaki 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. – BackSlash Sep 22 '16 at 08:37
  • please check this, I see there are lot more steps which need to be followed while using log4j with tomcat8 https://tomcat.apache.org/tomcat-8.0-doc/logging.html – piyushj Sep 22 '16 at 08:48
  • @piyushj 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. – BackSlash Sep 22 '16 at 08:53
  • Could you post the log4j configuration file? – Little Santi Sep 22 '16 at 09:41
  • @LittleSanti Sure, I edited the question – BackSlash Sep 22 '16 at 10:34
  • 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? – Little Santi Sep 22 '16 at 10:39
  • @LittleSanti I managed to deploy [this webapp](https://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/) without issues, but it doesn't use log4j :/ – BackSlash Sep 22 '16 at 10:45
  • @BackSlash did you find the solution ? am also facing the same issue.Thanks in advance – Spartan Apr 03 '19 at 04:59
  • @Thanga Unfortunately no. – BackSlash Apr 03 '19 at 06:06
  • @BackSlash and Thaga, did you find the solution ? I am also facing the same issue. – Created Bylucky Jan 02 '21 at 03:03

3 Answers3

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.

Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • Yeah, it happened to me too before, but that's not the case, Maven Dependencies are there, with `WEB-INF/lib` as Deploy Path – BackSlash Sep 22 '16 at 08:29
  • @BackSlash And log4j is included in that `Maven Dependencies` entry, sure? – Little Santi Sep 22 '16 at 08:31
  • 2
    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. – BackSlash Sep 22 '16 at 08:34
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.

Ucha
  • 41
  • 8
  • 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. – BackSlash Aug 01 '18 at 11:19
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.

Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • Tomcat is a fresh install, version 8.0.37 downloaded yesterday from the Apache site. These are the libraries: http://i.stack.imgur.com/nr2F2.png – BackSlash Sep 22 '16 at 08:45