Cannot delete slf4j dependencies - java

I have a problem with slf4j dependencies.
On POM.xml, i declared this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
But i meet this error message:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/C:/Users/r.rossi/.m2/repository/org/slf4j/slf4j-
nop/1.5.3/slf4j-nop-1.5.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/Users/r.rossi/.m2/repository/org/slf4j/slf4j-
jdk14/1.5.6/slf4j-jdk14-
1.5.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/Users/r.rossi/.m2/repository/org/slf4j/slf4j-
log4j12/1.7.12/slf4j-log4j12-
1.7.12.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.
On those directorties and on maven dependencies i have 1.5 versions but i don't have them on POM.xml file (as you can see), so i don't know how delete them. If i delete the directories, it works but the next esecution the project download again the 1.5 versions. How can i delete those damned 1.5 versions?

As the error stack suggest check the link Multiple Binding.
Also, check Dependency Tree to check which external dependency is trying to include (1.5.5 or earlier version).
Basically, your exclusion code
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
should be inserted inside that dependency which depends on an earlier version 1.5.5.

Related

How do I fix "SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"."

I have an application that has the following error message:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
This message occurs after I attempt to run any script for the application. I have found that, as it says, it does not create logs, so I have come up empty handed when something fails.
I am running this in Amazon Linux 2 which is closest to CentOS and Redhat.
I have found the following resources:
This issue is addressed here: http://www.slf4j.org/codes.html#StaticLoggerBinder
I can get the jar I need from here: https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.6.2/slf4j-simple-1.6.2.jar
After taking this jar and dropping it into my application's /lib, nothing changes.
Other articles describe adding this file to the class path. In Linux, I get this:
# java -classpath /opt/opendj/lib/slf4j-simple-1.6.2.jar org.slf4j.impl.StaticLoggerBinder
Error: Could not find or load main class org.slf4j.impl.StaticLoggerBinder
# java -jar /opt/opendj/lib/slf4j-simple-1.6.2.jar org.slf4j.impl.StaticLoggerBinder
no main manifest attribute, in /opt/opendj/lib/slf4j-simple-1.6.2.jar
Am I trying to add it to the class path right?
If needed, you can reproduce this issue by doing the following:
Steps to reproduce the behavior:
Install a fresh version of OpenDJ onto CentOS or Amazon Linux2 EC2 Instance. Install java 1.8.0
specifically java-1.8.0-openjdk
Install the server in any configuration, then run a status script.
Expected behavior
Logs should generate and no warning message can be presented.
Firstly, you should take a look here : https://www.slf4j.org/codes.html
Also,you can try adding these SLF4J maven dependencies into your pom and let me know if this works:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
Note: Consider that the maven dependencies versions are being updated, then maybe you prefer use the lates version of the dependencies(for example for log4j-api is 2.17.0)
On the other hand, this post could help you as well:
https://mkyong.com/java/log4j2-failed-to-load-class-org-slf4j-impl-staticloggerbinder/
In my case I was getting this error while using MavenCli lib. I had missed to add maven-compat lib in pom.xml. Below is full sample code I had used.
Sample code :
MavenCli mavenCli=new MavenCli();
mavenCli.doMain(new String[]{"clean","install"}, path_of_project_dir, System.out, System.out);
Dependency required :
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.6.3</version>
</dependency>
In my case, I was using the logback in a spring-boot app and faced this issue. It was related to incompatible versions of slf4j and logback which were coming from third party libraries. I excluded all of them and used these:
<!-- logging dependencies-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
${logback.version} and ${slf4j.version} are coming from spring-boot-dependencies-2.X.X.pom file.
I hope it will help :)
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
</dependency>

Why adding a dependency to slf4j-log4j12 does not make slf4j work properly?

I have a project using slf4j. So I need to provide an underlying framework.
I wanted to use log4j, so I specified the following dependency in my pom.xml, as specified here :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-beta2</version>
</dependency>
But I still have the following error message :
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
which shouldn't appear anymore, according to this.
If I change the dependency to this one, found on the net, it works :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
Does anyone have an explanation ?
The issue simply came from using a beta version of slf4j. Specifying a dependency to a stable version makes it work correctly :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder", nothing helps

I created default Drools project, tried to launch it, but I have this error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
what's wrong? I didn't change anything in project. Btw I have no pom.xml file, should I create one?
I checked some solutions, but nothing helps, please help
Add a runtime dependency to logback-classic. Or if you don't use maven/gradle/etc, then add the logback-classic jar in your classpath.
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
See http://www.slf4j.org for more information and other options.

SLF4J: Class path contains multiple SLF4J bindings; exclude slf4j

I am trying to load a Video using OpenIMAJ. It does Display the Video but always shows me this Error:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/MaryLu/.m2/repository/ch/qos/logback/logback-classic/1.0.0/logback-classic-1.0.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/MaryLu/.m2/repository/org/slf4j/slf4j-log4j12/1.7.2/slf4j-log4j12-1.7.2.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 [ch.qos.logback.classic.selector.DefaultContextSelector]
16/04/24 20:39:57 INFO xuggle.XuggleVideo: URL file:/F:/workspaceMaven/keyboardcat.flv could not be opened by ffmpeg. Trying to open a stream to the URL instead.
20:39:57.984 [main] DEBUG com.xuggle.xuggler - Could not open output url: file:/F:/workspaceMaven/keyboardcat.flv (../../../../../../../csrc/com/xuggle/xuggler/Container.cpp:436)
I already checked Similar Questions but I didn't manage to solve the problem. If i understood, i need to exclude slf4j somewhere in my POM.xml but i don't really know in which dependency.
Already the answer is given in your log file. Follow the tutorial.
http://www.slf4j.org/codes.html#multiple_bindings
As Wouter stated
Run mvn dependency:tree and search which dependency have the slf4j implementations you do not want, then exclude them with a dependency exclusion like:
<dependency>
<groupId>org.someexternallib</groupId>
<artifactId>someexternallibartifact</artifactId>
<version>...</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
The slf4j messages is just telling you that your slf4j configuration is broken so it falls back to a default configuration.
The real problem is that your URL does not point to a local file as you apparently expect it to because it is broken, so the code trying to use it fails. A possible reason for the brokenness might be if you have the file open already somewhere, so Windows will not allow it to be overwritten.
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-simple</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
In order to find the dependencies that use above artifact, find out by
"$ mvn dependency:tree"
once you find that out put the above exclusions on the corresponding dependencies. That should fix it, I would say pay attention on specific dependencies.

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory

I am facing this error while running my GWT application.
I have these jar files in my classpath: slf4j-api & slf4j-log4j12
Any idea what could be the reason?
This problem is due to a change in slf4j-log4j12 jar. From version 1.5.6 it doesn't allow to access the field org.slf4j.impl.StaticLoggerBinder.SINGLETON.
To resolve it, use the newest jars (or at least version 1.5.6 onward) for both slf4j-api & slf4j-log4j12.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
Finally resolved this problem in my SpringBoot application. If updating version is not helping this might help. Sometimes other libraries might bring different versions of this dependencies. These are the steps:
By error stack trace figure out which dependency is giving this issue
Get maven dependency plugin tree. Using this tree details find out if this library is coming as part of the some other dependency. In my case, the logback-classic and log4j-over-slf4j were giving this problem. They came together under spring-boot-starter-web
Use <exclusions><exclusion></exclusion></exclusions> in your pom.xml in that dependency for the libraries that giving this issue. In my case it looks like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
References:
http://www.slf4j.org/faq.html#IllegalAccessError
http://www.slf4j.org/codes.html#multiple_bindings

Categories