使用maven构建项目时,如果项目中有log4j的依赖,在运行程序时可能会出现在同一个类中log4j-over-slf4j.jar和 slf4j-log4j12.jar冲突的问题:

项目报错内容为:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.apache.log4j.LogManager.getLogger(LogManager.java:44)
    at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:66)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:657)
    at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:173)

Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
    at org.apache.log4j.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:49)
    ... 8 more

原因是:log4j-over-slf4j和slf4j-log4j12是跟Java日志系统相关的两个jar包,当它们同时出现在classpath下时,就可能会引起堆栈溢出异常。

解决办法:pom.xml文件中,在使用到的依赖包中添加<exclusion>标签将slf4j-log4j12.jar排除即可:

例如:红色字体是排除的内容

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0</version>
                <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

log4j-over-slf4j.jar AND slf4j-log4j12.jar 依赖冲突解决方案的更多相关文章

  1. log4j.xml 为什么要使用SLF4J而不是Log4J

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...

  2. java日志框架log4j详细配置及与slf4j联合使用教程

    最后更新于2017年02月09日 一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” <d ...

  3. [转载]java日志框架log4j详细配置及与slf4j联合使用教程

    一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...

  4. java日志框架log4j详细配置及与slf4j使用教程

    一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...

  5. eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序

    一.新建maven的java工程 1.eclipse里file-new-other,选择maven Project 2.选中 Use default Workspace location,然后 nex ...

  6. 使用log4j出现缺失com.sun.jdmk:jmxtools:jar:1.2.1

    用maven引用log4j出现缺失com.sun.jdmk:jmxtools:jar:1.2.1的错误提示 解决方案一:使用1 .2 .15之前版本的log4j 解决方二: <dependenc ...

  7. Java学习-051-Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError

    错误信息:Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting St ...

  8. 在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

    下面是我的项目的build.gradle文件的所有内容,注意高亮部分是关键: group 'com.xbs' version '1.0-SNAPSHOT' apply plugin: 'java' a ...

  9. Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path 解决过程

    原因:log4j-over-slf4j和slf4j-log4j12是跟Java日志系统相关的两个jar包,如果同时出现,就可能会引起堆栈异常 解决:找到依赖冲突发生位置,排除一个即可. 问题是 如何找 ...

随机推荐

  1. element ui 在vue中使用可能遇到的问题

    组件使用参照官网http://element.eleme.io/#/zh-CN/component/quickstart 在 main.js 中写入以下内容: import Vue from 'vue ...

  2. leetcode_Stone Game_dp_思维

    Alex和Lee玩游戏,共有偶数堆石头,石头总数为奇数,两人每次要么拿第一堆,要么拿最后一堆,两人以最优策略拿石堆(一次拿走完整的一堆),Alex先手,Alex赢返回True,否则返回False. 思 ...

  3. 【git】搭建git服务器

    在 Linux 下搭建 Git 服务器 目录 ① 安装 Git ② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码 ③ 服务器端创建 Git 仓库 ④ 客户端 clon ...

  4. 如何修改MFC的图标

    原文:如何修改MFC的图标 修改左上角的图标和任务栏里图标 在对话框构造函数中 CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestD ...

  5. hdfs深入:08、hdfs的JavaAPI以及如何解决winutils的问题

    /** * 通过url注册的方式访问hdfs,了解,不会用到 * @throws Exception */ 以下为详细代码://1.注册hdfs的url,让java代码能识别hdfs的url形式URL ...

  6. 基于Docker Compose搭建mysql主从复制(1主2从)

    系统环境 * 3 Ubuntu 16.04 mysql 8.0.12 docker 18.06.1-ce docker-compose 1.23.0-rc3 *3 ==> PS  ###我用的是 ...

  7. Go:数组

    package main import "fmt" func main() { // 定义数组的方式 var array1 [3]int array2 := [3]int{} fm ...

  8. 【Html,Css,JavaScript】初学总结

    网页制作 HTML 一.通用模板: <!DOCTYPE html> <html lang=”en”> <head> <meta  charset=”UTF-8 ...

  9. 树莓派--bcm2835 library (2) 交叉编译BCM2835

    在上文中,按照guide, 在树莓派目标板上install bcm2835. 因为bcm2835是用户空间应用,所以可以在宿主机上交叉编译,生成binary后在树莓派执行 按照guide: Insta ...

  10. python爬虫框架—Scrapy安装及创建项目

    linux版本安装 pip3 install scrapy 安装完成 windows版本安装 pip install wheel 下载twisted,网址:http://www.lfd.uci.edu ...