問題描述:學習 Hibernate持久化技術時,使用hibernate-annotations-3.4.0.GA版本做實驗,做好了程序和配置文件后,運行程序出現 java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory錯誤。
問題原因:jar文件版本沖突
類 org.slf4j.impl.StaticLoggerBinder在slf4j-api 中是類的公有靜態變量:
public static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
以上是在網絡上找的答案,也有很多人試過的,但是我遇到這個問題時,使用的Hibernate是3.6的,這種方法不適合。
后來我將 hibernate-jpa-2.0-api-1.0.0.Final.jar 包也加了進來之后,程序就好了。具體原因還在糾結.
而在slf4j-log4j12(slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar其中之一)中確是私有變量:
private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
解決方案:1.修改slf的源代碼,將這個變量有私有改為公有,再打包,問題可解決。
2.在類路徑先將slf4j-api.jar 刪除,再導入同版本的slf4j-api-1.5.6.jar 和slf4j-log4j12-1.5.6.jar ,問題可解決。