1. 程式人生 > >SLF4J: Failed to load class的問題及解決

SLF4J: Failed to load class的問題及解決

今天在用Maven搞一個工程,安裝要求我添加了所有需要的依賴,可是一執行測試程式,就跳出這樣一個大大的錯誤:

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
……

原來這是因為sl4j和log4j的不相容導致的,具體處理方案如下:
首先看看你工程中的sl4j-api的版本(比如我的是1.5.11),然後在

http://mvnrepository.com/搜尋slf4j-log4j12,會出現SLF4J LOG4J 12 Binding,點選進入,會有很多版本的slf4j-log4j12,我們點選1.5.11版本的slf4j-log4j12進入詳細資訊頁面,檢視依賴的log4j,這個版本的slf4j-log4j12依賴的是1.2.14版本的log4j。

所以,我們在我們的工程中新增1.5.11版本的slf4j-log4j12和1.2.14版本的log4j,問題完美解決。

This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

如果這樣就修改了以後還是會報錯,將SLF4J換成高版本試試!