Java 多个 SLF4J 绑定会导致错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19656496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:11:04  来源:igfitidea点击:

Multiple SLF4J bindings cause error?

javamavendependenciesslf4j

提问by Biffy

I have a problem with my dependency tree and multiple SLF4J binding. 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 绑定有问题。到目前为止,我发现这通常只会导致警告,但在我的情况下,它似乎阻止了我的程序运行:这些是我得到的例外情况:

SLF4J: Class path contains multiple SLF4J bindings. 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_bindingsfor an explanation. SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding. SLF4J: Your binding is version 1.5.5 or earlier. SLF4J: Upgrade your binding to version 1.6.x. or 2.0.x Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;

SLF4J:类路径包含多个 SLF4J 绑定。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(或更高版本)与此绑定不兼容。SLF4J:您的绑定版本为 1.5.5 或更早版本。SLF4J:将您的绑定升级到 1.6.x 版。或 2.0.x 线程“main”中的异常 java.lang.NoSuchMethodError: 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?

有人可以告诉我如何解决这个问题吗?

采纳答案by Ankur Shanbhag

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.
  • 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. These dependent jar may refer to different versions of slf4j jars which causes your application to fail. In such cases, make sure that you have the jar with higher version of SLF4j added before other jar using SLF4J jars. This will make sure that your java program will pick up the latest version of the SLF4J which obviously is backward compatible.
  • 如果您的类路径中有几个具有不同版本的slf4j jar,请确保只包含一个 slf4j jar(可能带有更高版本)。
  • 有时可能无法排除多个 slf4j jar,因为它们可能会在您的类路径上被其他 jar 内部使用。这些依赖的 jar 可能指的是不同版本的 slf4j jar,这会导致您的应用程序失败。在这种情况下,请确保在使用 SLF4J jars 的其他 jar 之前添加了具​​有更高版本 SLF4j 的 jar。这将确保您的 java 程序将选择最新版本的 SLF4J,这显然是向后兼容的。

回答by t.rakesh

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 有更多的依赖。

回答by Fateh

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>

回答by Hamza

This is occur when there is more than one 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 . 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 . :)

当有多个罐子时会发生这种情况。要检查 jar 是否已经可用,请转到项目 -> java 资源 -> maven 依赖项并检查 jar 是否已经可用。如果它可用并且您仍然收到错误。然后在 .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. 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 文件,我也会遇到错误。然后我需要从我的 pom.xml 中删除它并从谷歌下载那个 jar 并将其导入我的项目。确保如果你这样做然后不要忘记去项目属性 - >部署程序集选项卡 - >单击添加 - > Java 构建路径条目并单击该 jar 文件并单击应用。

回答by Akanksha Sharma

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”依赖项。

回答by Hans Pour

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

  2. find out which library is using 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:

         <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>
    
  1. mvn dependency:tree

  2. 找出哪个库正在使用 slf4j:

    [INFO] +- net.lightbody.bmp:browsermob-proxy:jar:2.0-beta-8:compile
    [INFO] |  +- org.slf4j:slf4j-jdk14:jar:1.7.25:compile
    
  3. 像这样从 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>