Reputation: 195
I don't know what is the problem with the program. I got this notice. I have set the dependencies correctly on pom.xml file.
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.
pom.xml
..
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
..
Upvotes: 1
Views: 2507
Reputation: 77951
One simple way to avoid this warning is to add the following runtime dependency to your project's POM
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>runtime</scope>
</dependency>
This will provide a simple logging implementation that will print your INFO level statements. You'll quickly outgrow this solution, but it allows time to investigate the logging framework your code will eventually use.
SLF4J is a framework designed to hide the details of how your logging is actually implemented. For example at runtime you could decide to use log4j, commons logging, Java logging or logback.
The following Java project reproduces your issue:
|-- pom.xml
`-- src
`-- main
`-- java
`-- HelloWorld.java
Run program from Maven as follows:
$ mvn exec:java -Dexec.mainClass=HelloWorld
..
..
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.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Once you add the missing dependency into your POM, your program will then have a logging implementation, which it can use to issue it's logging statements as follows:
$ mvn exec:java -Dexec.mainClass=HelloWorld
..
..
[HelloWorld.main()] INFO HelloWorld - Java says: Hello world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.demo</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</project>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
Logger log = LoggerFactory.getLogger(HelloWorld.class);
public String speak(String speech) {
log.info("Java says: {}", speech);
return speech;
}
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
hello.speak("Hello world");
}
}
If you configure Maven to create an executable jar then it will automatically package the jar with a classpath referencing the dependencies listed in your POM file.
$ mvn clean package
$ java -jar target/my-project-1.0-SNAPSHOT.jar
[main] INFO HelloWorld - Java says: Hello world
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.demo</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Upvotes: 3
Reputation: 11298
It seems slf4j jars are missing in your class path. Download slf4j libraries and add it in your project class path.
Upvotes: -1