java.lang.NoClassDefFoundError:Lorg / apache / logging / log4j / Logger;但是工件存在

时间:2016-09-22 08:20:23

标签: java maven tomcat classnotfoundexception

我正在使用Tomcat部署java webapp。

我得到一个很长的堆栈跟踪,简而言之:

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
[...]

现在,错误很明显。出于某种原因,log4j包不在类路径中。

该应用程序是一个maven webapp,pom.xml是这样的:

<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>

如果我转到项目属性,在Libraries-&gt; Maven Dependencies下我看到了:

enter image description here

找到,下载了库,它们位于类路径中。

我还试图在我的项目中打开一个java类并声明

org.apache.logging.log4j.Logger Logger;

没有错误,找到Logger界面。

这里发生了什么?为什么即使库在类路径中,Tomcat也无法启动?

编辑 - 这是log4j配置文件:

<?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>

3 个答案:

答案 0 :(得分:9)

根据您的描述,我假设您正在使用Eclipse。

嗯,你最好去Project properties - &gt; Deployment assembly并确保包含maven dependencies条目。

我经常尝试在执行Maven - &gt;时错过此配置Update Project

答案 1 :(得分:2)

我正在使用Eclipse,每次在pom.xml中进行更改时,我都会遇到相同的问题。我不知道为什么,但是Eclipse删除了Maven依赖项。

解决方案:严格单击项目,选择属性,选择 Deployment Assembly ,然后在“源”列中验证名为“ Maven Dependencies”的行”。如果不存在,请单击添加... Java构建路径条目,然后单击 Maven依赖项。最后应用并关闭

答案 2 :(得分:0)

确保log4j库中未包含log4j库(也不包括已签名的目录),因为它可能会产生有关类加载策略的冲突。