26

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: 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_bindings for 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;

and this is the relevant piece of my dependencies: 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?

Biffy
  • 871
  • 2
  • 10
  • 21
  • You're including `slf4j-log4j12` multiple times (either directly or indirectly through another dependency) with different versions (1.5.3 and 1.6.1, according to the error message). – Jesper Oct 29 '13 at 11:09
  • https://www.slf4j.org/codes.html#multiple_bindings – Shachi Mar 13 '18 at 09:19

7 Answers7

33

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.
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • @Ankur And how do I ensure in eclipse that it will pick up latest one? – Dangling Piyush Nov 12 '14 at 13:18
  • 3
    Specify the latest one first in your project build path. To verify the order check .classpath file of your project. It will show the order in which jar files are added. – Ankur Shanbhag Nov 13 '14 at 06:06
  • My problem is that, I have two same versions of slf4j jars added to my classpath (I don't have control to avoid that). For normal working of the program it doesn't matter, but SLF4j thinks there are two different bindings and doesn't work, even though both bindings are of version 1.4.2. Anyway to trick slf4j into picking up one? – endless Aug 23 '15 at 15:12
  • @endless : If you could explicitly add version 1.4.2 before any other jar, it should resolve the issue. – Ankur Shanbhag Aug 23 '15 at 21:31
12

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:

  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>
    
Hans Pour
  • 432
  • 6
  • 15
5

If your project has dependency on other project and the other one use slf4j as well with different version try to use excusion

<exclusions>
   <exclusion>
       <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </exclusion>
</exclusions>
Noa
  • 315
  • 1
  • 7
  • 31
2

I would suggest to use following dependency in maven instead,

<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.

Drunix
  • 3,313
  • 8
  • 28
  • 50
t.rakesh
  • 31
  • 1
1

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.

Kedaar Rao
  • 127
  • 1
  • 6
0

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 . :)

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 .

Hamza
  • 1
  • 2
0

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'.