首页 > 解决方案 > 如何避免 SLF4J 类路径中的多个类绑定

问题描述

我有一个使用 .jar 打包到 jar 中的 scala 代码sbt。我把那个罐子放在下面$SPARK/jars并试图使用我得到的类

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/tmp/hadoop-ubuntu/nm-local-dir/usercache/ubuntu/filecache/87/__spark_libs__5108127367259158326.zip/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/ubuntu/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]

在阅读了关于 SO 的建议后,我在我的中添加了以下内容,build.sbt以防止额外绑定slf4j

libraryDependencies ++= Seq(
  "org.slf4j" % "slf4j-api"       % "1.7.7",
  "org.slf4j" % "jcl-over-slf4j"  % "1.7.7"
).map(_.force())

libraryDependencies ~= { _.map(_.exclude("org.slf4j", "slf4j-jdk14")) }

这没有解决问题,所以我尝试了另一种解决方案,我在这里找到了,并将以下内容添加到build.sbt文件中

libraryDependencies ++= Seq(
    //depencies
).map(_.exclude("org.slf4j", "*"))

//insert one without exclusion
libraryDependencies ++= Seq(
  "ch.qos.logback" % "logback-classic" % "1.1.3"
)

这不起作用并导致错误。

我检查了file:/tmp/hadoop-ubuntu/n...位置,只有在我开始新的 spark 会话时才会创建它。我该如何解决这个问题?

标签: scalaapache-sparkjarsbtslf4j

解决方案


尝试Provided

libraryDependencies ++= Seq(
  "org.slf4j" % "slf4j-api"       % "1.7.7" % Provided,
  "org.slf4j" % "jcl-over-slf4j"  % "1.7.7" % Provided
)

这应该排除他们。

也许你必须清除缓存/tmp/hadoop-ubuntu/nm-local-dir/usercache/ubuntu/filecache/


推荐阅读