首页 > 代码库 > java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory

 一、问题描述:

搭建hibernate环境时,使用hibernate-distribution-3.3.1.GA-dist和slf4j-1.7.7。配置文件和程序OK之后,运行程序出现如下问题:

 1 java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
 2     at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
 3     at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:151)
 4     at com.pers.test.CustomerTest.<clinit>(CustomerTest.java:33)
 5     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 6     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 7     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 8     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 9     at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
10     at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
11     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
12     at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
13     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
14     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
15     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
16     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
17     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
18     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
19     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
20     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
21     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
22     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
23     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
24     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
25     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
26     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

二、原因分析:jar包版本冲突

类 org.slf4j.impl.StaticLoggerBinder在hibernate-distribution-3.3.1.GA-dist\hibernate-distribution-3.3.1.GA\lib\required\slf4j-api-1.5.2.jar中是类的公有静态变量: 

public static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();

而在slf4j-1.7.7\slf4j-log4j12-1.7.7.jar中是私有变量: 

private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();

解决方案:1.修改slf的源代码,将这个变量由私有改为公有,再打包,问题可解决。

                 2. 将slf4j-api-1.5.2.jar替换为slf4j-1.7.7\slf4j-api-1.7.7.jar,问题可解决。

 

三、jar包附图

技术分享

  如果需要注解或和其他框架整合,则另行导入,各jar作用不再赘述。

 

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory