SpringBoot使用Log4j2出现异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36553913/article/details/80539104

异常信息

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
    at com.example.log4j2.Log4j2Application.main(Log4j2Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

pom.xml中的依赖配置

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency> <!-- 引入log4j2依赖 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

通过依赖配置可以看出,使用的是log4j2的2.0.0.RELEASE版本,springboot是1.5.14.BUILD-SNAPSHOT,出错的地方就在这里,出现版本后依赖jar包没有正常引入,在启动时SpringApplication中的Logger使用报错,缺少了jar包依赖,具体代码为:

private static final Log logger = LogFactory.getLog(SpringApplication.class);

解决方法

将log4j2的版本信息去掉,就可正常启动

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页