. Advertisement .
. Advertisement .
“Slf4j: class path contains multiple slf4j bindings.” is a common error that many programmers encounter. It happens in many ways. What is the cause of this error and how to fix it? Let’s read this article to find the best answer.
When do you get the error “slf4j: class path contains multiple slf4j bindings.”?
When you run the program, you easily encounter this warning:
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]
This notice informs us that SLF4J has discovered two bindings. Both are contained in the files slf4j-log4j12-1.7.21.jar and logback-classic-1.1.7.jar, respectively.
For many logging frameworks, the Simple Logging Façade for Java (SLF4J) acts as a straightforward facade or abstraction. At deployment time, you can plug in the preferred logging framework. SLF4J searches the classpath for bindings (also known as providers) to do this. In essence, bindings are variations of a certain SLF4J class designed to be expanded to add in a particular logging framework. SLF4J will only connect to one logging framework at a time by design. As a result, it will issue a warning if there are several bindings on the classpath.
Remember that no embedded component, including a library or framework, should ever declare a dependent on any SLF4J binding. This is so that the end user is forced to use the binding when a library declares a compile-time dependency on an SLF4J binding. This obviously defeats the fundamental goal of SLF4J. As a result, they ought to only rely on the slf4j-api library. It’s crucial to remember that this is just a warning. SLF4J will select one logging framework from the list and bind with it if it discovers several bindings. As noted on the last line of the warning, SLF4J selected Log4j by using the real binding of org.slf4j.impl.Log4jLoggerFactory.
How to solve the error “slf4j: class path contains multiple slf4j bindings.”?
Option 1: Remove the slf4j-log4j12 JAR
To solve the error “slf4j: class path contains multiple slf4j bindings.”, you need to remove the slf4j-log4j12 JAR from the docx4j dependent now that you are aware of the problematic dependence:
It could be a good idea to eliminate Log4j because you won’t use it.
Option 2: Locate a conflicting jar
Another solution for you is to determine the true reason of the warning. To do that you must locate a conflicting jar.
The conflicting jar can be located using the following command.
This will show the project’s dependency tree:
[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
There are two slf4j bindings present, as you can see.
Because of spring-boot-starter-web, the transitive dependency ch.qos.logback:logback-classic:jar org.apache.logging.log4j:log4j-slf4j-impl:jar logback-classic was fetched. You have specifically added log4j-slf4j-impl so that your app could use log4j.
Option 3: Remove the undesirable dependencies
Removing the undesirable dependencies from the pom.xml file is a great way for you to solve the error “slf4j: class path contains multiple slf4j bindings.” Logback-classic is not included in this example. Look at following:
<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>
Run the program while the maven project is updated. You will not get the error “slf4j: class path contains multiple slf4j bindings.”
The solutions mentioned above are great ways for you to fix your problem. Although they are simple, they work flawlessly for you. So, what are you waiting without applying them to get your desired results?
The remedies presented above have been the most effective for individuals who are still perplexed by this error “slf4j: class path contains multiple slf4j bindings.” When you still seek help or have other questions, we have a big community where everybody is always willing to help you. Lastly, we hope all readers have a fantastic day with innovative code solutions.
→ “ERROR org.springframework.boot.SpringApplication – Application run failed org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property ‘spring.profiles.active’ imported from location ‘class path resource [application-dev.yml]’ is invalid in a profile specific resource” In Java