【问题标题】:I'm getting "NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil"我收到“NoClassDefFoundError:org/apache/logging/log4j/util/ReflectionUtil”
【发布时间】:2019-03-13 00:55:03
【问题描述】:

我的 build.gradle 文件中有以下依赖项。

compile 'org.slf4j:slf4j-api:1.7.25'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'

在运行我的单元测试时,会显示以下日志。

exclude patterns:SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in 
[jar:file:....gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.7/382b070836b8940a02d28c936974db95e9bfc3a4/log4j-slf4j-impl-2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/z002qz1/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.9.1/a97a849b18b3798c4af1a2ca5b10c66cef17e3a/log4j-slf4j-impl-2.9.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)

我正在使用 springboot 2.0.4.RELEASE。我希望这只是某种版本不匹配问题。任何见解都值得赞赏。

【问题讨论】:

标签: java spring-boot gradle log4j slf4j


【解决方案1】:

错误:java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil

是因为自 2.9.0 版以来的 log4j2 已从 api jar (log4j-api-2.x.x.jar) 中删除了该类。

拥有它的最后一个版本是 2.8.2

您可能在类路径中有混合版本。

【讨论】:

  • 你好 devwebcl,那么使用最新版本的 Log4j 的方法是什么?我没有得到混合版本的原因。
  • 你的类路径中只需要一个版本的 log4j2,你不能在同一个类路径中同时拥有 2.8.2(或更低)和 2.9.0+。否则你可能会遇到 NoClassDefFoundError。因此,请尝试仅使用 log4j2 的最新版本:2.11.2
【解决方案2】:

在spring boot中配置log4j2的正确方法是这样的:

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-log4j2'
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

我是explained in the documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多