Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

libraries

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.

like image 909
Dean013 Avatar asked Dec 08 '22 10:12

Dean013


2 Answers

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/

like image 71
mononoke83 Avatar answered Apr 14 '23 20:04

mononoke83


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>
like image 21
Akta Kalariya Avatar answered Apr 14 '23 20:04

Akta Kalariya