简体   繁体   English

多个 SLF4J 绑定会导致错误?

[英]Multiple SLF4J bindings cause error?

I have a problem with my dependency tree and multiple SLF4J binding.我的依赖树和多个 SLF4J 绑定有问题。 What I found out so far is that usually this only causes a warning but in my case it seems to prevent my program from running: These are the exceptions I get:到目前为止,我发现通常这只会导致警告,但在我的情况下,它似乎会阻止我的程序运行:这些是我得到的异常:

SLF4J: Class path contains multiple SLF4J bindings. SLF4J:类路径包含多个 SLF4J 绑定。 SLF4J: Found binding in [jar:file:/C:/Users/FischerNi/.m2/repository/org/slf4j/slf4j-jdk14/1.5.3/slf4j-jdk14-1.5.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/FischerNi/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J:在 [jar:file:/C:/Users/FischerNi/.m2/repository/org/slf4j/slf4j-jdk14/1.5.3/slf4j-jdk14-1.5.3.jar!/org/slf4j/ 中找到绑定impl/StaticLoggerBinder.class] SLF4J:在 [jar:file:/C:/Users/FischerNi/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar 中找到绑定!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J:有关说明,请参见http://www.slf4j.org/codes.html#multiple_bindings SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding. SLF4J:slf4j-api 1.6.x(或更高版本)与此绑定不兼容。 SLF4J: Your binding is version 1.5.5 or earlier. SLF4J:您的绑定是 1.5.5 或更早版本。 SLF4J: Upgrade your binding to version 1.6.x. SLF4J:将您的绑定升级到版本 1.6.x。 or 2.0.x Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;或线程“主”java.lang.NoSuchMethodError 中的 2.0.x 异常:org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;

and this is the relevant piece of my dependencies: net.lightbody.bmp browsermob-proxy 2.0-beta-8这是我依赖项的相关部分:net.lightbody.bmp browsermob-proxy 2.0-beta-8

    <!-- LOGGING DEPENDENCIES - LOG4J -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>

Can somebody please tell me how to resolve this issue?有人可以告诉我如何解决这个问题吗?

There are couple of solutions to this: 有几种解决方案:

  • Make sure that you include only one slf4j jar (probably with the higher version) if you have couple of them with different versions on your class path. 如果你的类路径中有几个不同的版本,请确保只包含一个slf4j jar (可能是更高版本)。
  • Sometimes it may not be possible to exclude multiple slf4j jars, as they may be used by other jars internally, which are on your class path. 有时可能无法排除多个slf4j罐子,因为它们可能被内部的其他罐子使用,这些罐子位于类路径上。 These dependent jar may refer to different versions of slf4j jars which causes your application to fail. 这些依赖jar可能会引用slf4j jar的不同版本,这会导致应用程序失败。 In such cases, make sure that you have the jar with higher version of SLF4j added before other jar using SLF4J jars . 在这种情况下,请确保在使用SLF4J罐的其他罐之前添加了更高版本的SLF4j的jar This will make sure that your java program will pick up the latest version of the SLF4J which obviously is backward compatible. 这将确保您的java程序将获取最新版本的SLF4J,这显然是向后兼容的。

If your project has dependency on other project and the other one use slf4j as well with different version try to use excusion 如果你的项目依赖于其他项目,另一个使用slf4j以及不同的版本尝试使用excusion

<exclusions>
   <exclusion>
       <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </exclusion>
</exclusions>

Answer from Fateh is correct I had to spend some time to figure out how to use it, that's why I'm adding a complete solution: 来自Fateh的回答是正确的我不得不花一些时间来弄清楚如何使用它,这就是为什么我要添加一个完整的解决方案:

  1. Run mvn dependency:tree 运行mvn dependency:tree

  2. find out which library is using slf4j: 找出哪个库正在使用slf4j:

     [INFO] +- net.lightbody.bmp:browsermob-proxy:jar:2.0-beta-8:compile [INFO] | +- org.slf4j:slf4j-jdk14:jar:1.7.25:compile 
  3. exclude it from maven like this: 像这样从maven中排除它:

      <dependency> <groupId>net.lightbody.bmp</groupId> <artifactId>browsermob-proxy</artifactId> <version>2.0.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> </exclusion> </exclusions> </dependency> 

I would suggest to use following dependency in maven instead, 我建议在maven中使用以下依赖,

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

This has solved my problem although I have more dependencies using slf4j. 这解决了我的问题,虽然我有更多依赖使用slf4j。

This is occur when there is more than one jar. 当有多个jar时会发生这种情况。 To check if there jar is already available or not go to project -> java resources -> maven dependencies and check if the jar is already available there or not . 要检查jar是否已经可用,请转到项目 - > java resources - > maven dependencies并检查jar是否已经可用。 If its available and still you get the error . 如果它可用,你仍然得到错误。 Then find the location of that jar file in .m2\\resources folder and delete complete folder related to that jar file then download a newer version and import it to your project . 然后在.m2 \\ resources文件夹中找到该jar文件的位置,并删除与该jar文件相关的完整文件夹,然后下载更新的版本并将其导入到您的项目中。 :) :)

Sometime i get errors even i have download the right jar file with a right version in my pom.xml file. 有时我甚至出错了,即使我在pom.xml文件中下载了正确版本的正确jar文件。 Then i need to remove it from my pom.xml and download that jar from google and import it to my project.Make sure if you do it then don't forget to go Project properties ->Deployment Assembly tab->Click Add ->Java Build Path Entries and Click on that jar file and Click apply . 然后我需要从我的pom.xml中删除它并从谷歌下载该jar并将其导入我的项目。确保如果你这样做然后不要忘记去项目属性 - >部署程序集选项卡 - >单击添加 - > Java构建路径条目并单击该jar文件,然后单击“应用”。

When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. 如果类路径上有多个绑定,请选择一个且只能使用一个绑定,并删除其他绑定。

Try removing explicitly added dependency of 'org.slf4j' or 'log4j2'. 尝试删除'org.slf4j'或'log4j2'的显式添加依赖项。

I had a similar issue and none of the excludes worked despite excluding from all the dependencies.我有一个类似的问题,尽管排除了所有依赖项,但没有一个排除项起作用。 A simple reloading of all gradle projects did the trick for me.简单地重新加载所有 gradle 项目对我来说是诀窍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM