Fix the Error “slf4j: class path contains multiple slf4j bindings”

Java is a reputed and most-used object-oriented, class-based, and high-level language for creating mobile applications, web applications, game applications, and desktop applications. It is popular for building various applications in a simpler way. When programmers and developers want to build any application, they instantly open Java and start writing codes. It is also known as a server-side programming language. Maven is an open-source build automation tool is used for Java projects. It is developed by Apache Group to make programmers build, publish, and deploy many projects. On the other hand, slf4j is an acronym for Simple Logging Facade for Java, which works as an abstraction or facade for several logging frameworks. It is known for providing Java logging APIs. When you are working with slf4j, you may experience the error “slf4j: class path contains multiple slf4j bindings”.

You must be worried when you encounter the error as it involves Maven as well as slf4j. You don’t need to worry anymore as you are here at Tutopal. We will provide you with the solutions to make the error go away. First, let’s check out how the error occurs

How the error pops up

When working on slf4j, you figure out multiple logging frameworks are bound to slf4j, which returns an error message. You get the following error message

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:.../slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:.../logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

Three Easy Solutions to Fix the Error “slf4j: class path contains multiple slf4j bindings”

We come up with outstanding three solutions that can handle the error warning as well as quite easy to implement. Have a look at the solutions

Solution 1 – Remove unwanted dependencies

To fix the error, you need to remove the unwanted dependencies. It should be removed in pom.xml. have a look at the dependency (longback-classic) to remove

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
       </exclusion>
       <exclusion>
           <groupId>org.apache.logging.log4j</groupId>
           <artifactId>log4j-to-slf4j</artifactId>
       </exclusion>
    </exclusions>
</dependency>

It resolves the error. Now, you can execute the program without the error popping up.

Solution 2 – Delete the slf4j-log4j12 JAR

Another way to resolve the error is to delete the slf4j-log4j12 JAR found in docx4j. It is the dependency that is causing the error. So, removing it helps. You can simply remove log4j as it will not be used.

Solution 3 – Search for the conflicting JARs

To fix the error, you need to search for the conflicting JAR. To look for the conflicting jar, you need to use the below command

mvn dependency:tree

This will open the dependency tree of the project. This is how it looks:

[INFO] +- org.docx4j:docx4j:jar:3.3.5:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.21:compile
[INFO] | +- log4j:log4j:jar:1.2.17:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.7:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.1.7:compile

Logback is used for application login, and that’s the reason logback binding is deliberately added in the logback-classic jar.

Conclusion

We discussed the solutions to fix the error warning “slf4j: class path contains multiple slf4j bindings”. Choose the solution that is suitable for your project.

I hope it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *