本文介紹了無法加載類slf4g StaticLoggerBinder的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我不知道程序有什么問題。我收到了這張通知。我已正確設(shè)置pom.xml文件上的依賴項。
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>
..
推薦答案
避免此警告的一種簡單方法是將以下運行時依賴項添加到項目的POM
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>runtime</scope>
</dependency>
這將提供一個簡單的日志記錄實現(xiàn),它將打印您的INFO級別語句。這個解決方案很快就會過時,但它讓您有時間研究代碼最終將使用的日志記錄框架。
SLF4J是一個旨在隱藏日志記錄實際實現(xiàn)方式的細節(jié)的框架。例如,在運行時,您可以決定使用log4j、commons logging、Java logging或logback。
更新1:如何重現(xiàn)問題
以下Java項目會重現(xiàn)您的問題:
|-- pom.xml
`-- src
`-- main
`-- java
`-- HelloWorld.java
從Maven運行程序,如下所示:
$ 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] ------------------------------------------------------------------------
一旦將缺少的依賴項添加到POM中,您的程序?qū)碛幸粋€日志記錄實現(xiàn),它可以使用該實現(xiàn)發(fā)出如下所示的日志記錄語句:
$ mvn exec:java -Dexec.mainClass=HelloWorld
..
..
[HelloWorld.main()] INFO HelloWorld - Java says: Hello world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
pom.xml
<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>
HelloWorld.java
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");
}
}
更新2:如何創(chuàng)建可執(zhí)行JAR
如果將Maven配置為創(chuàng)建可執(zhí)行JAR,則它將使用引用POM文件中列出的依賴項的類路徑自動打包JAR。
$ mvn clean package
$ java -jar target/my-project-1.0-SNAPSHOT.jar
[main] INFO HelloWorld - Java says: Hello world
pom.xml
<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>
這篇關(guān)于無法加載類slf4g StaticLoggerBinder的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,