Hibernate Initial SessionFactory creation failed.java.lang.IllegalAccessError

wangwffgqeg 2010-10-27 01:14:11
刚刚学hibernate按官网给的教程出现
Initial SessionFactory creation failed.java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:10)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:22)
at org.hibernate.tutorial.EventManager.main(EventManager.java:15)
Caused by: java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:228)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:165)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
... 3 more
我查了一下错误原因是slf4j有问题
I am getting IllegalAccessError exceptions when using SLF4J. Why is that?
Here are the exception details.
Exception in thread "main" java.lang.IllegalAccessError: tried to access field
org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
This error is caused by the static initilizer of the LoggerFactory class attempting to directly access the SINGLETON field of org.slf4j.impl.StaticLoggerBinder. While this was allowed in SLF4J 1.5.5 and earlier, in 1.5.6 and later the SINGLETON field has been marked as private access.
If you get the exception shown above, then you are using an older version of slf4j-api, e.g. 1.4.3, with a new version of a slf4j binding, e.g. 1.5.6. Typically, this occurs when your Maven pom.ml file incoprporates hibernate 3.3.0 which declares a dependency on slf4j-api version 1.4.2. If your pom.xml declares a dependency on an slf4j binding, say slf4j-log4j12 version 1.5.6, then you will get illegal access errors.
To see which version of slfj4-api is pulled in by Maven, use the maven dependency plugin as follows.
mvn dependency:tree
If you are usig Eclipse, please do not rely on the dependency tree shown by m2eclipse.
In your pom.xml file, excplicitly declaring a dependecy on slf4j-api matching the version of the declared binding will make the problem go away.
这是官网给的解决方案,但不明白要怎么做,请教
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.hibernate.tutorials</groupId>
<artifactId>hibernate-tutorial</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>First Hibernate Tutorial</name>

<build>
<!-- we dont want the version to be part of the generated war file name -->
<finalName>${artifactId}</finalName>
</build>

<dependencies>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>1.5.8</version>
</dependency>

<!-- Because this is a web app, we also have a dependency on the servlet api. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>1.5.8</version>
</dependency>

<!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.8</version>
</dependency>

<!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>1.5.8</version>

</dependency>

</dependencies>

</project>
...全文
646 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
m690192206 2010-10-27
  • 打赏
  • 举报
回复
真不知道你把这个贴出来干什么,你先看看你的文件包缺不缺,按道理来说,你应该缺文件包,你仔细查查
madFatso 2010-10-27
  • 打赏
  • 举报
回复
1.修改slf的源代码,将这个变量有私有改为公有,再打包,问题可解决。



2.在类路径先将slf4j-api.jar 删除,再导入同版本的slf4j-api-1.5.6.jar 和slf4j-log4j12-1.5.6.jar ,问题可解决。

http://nomandia.javaeye.com/blog/402826

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧