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

异常信息

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.boot.SpringApplication.(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.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency> 
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
            <version>2.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        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.bootgroupId>
    <artifactId>spring-boot-starter-log4j2artifactId>
dependency>

SpringBoot使用Log4j2出现异常:Exception in thread

你可能感兴趣的:(异常处理)