Context initialization failed - java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

I received the following error while running a Spring and Hibernate application

Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
at org.slf4j.LoggerFactory.(LoggerFactory.java:60)
at org.hibernate.cfg.Configuration.(Configuration.java:151)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.class$(LocalSessionFactoryBean.java:158)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.(LocalSessionFactoryBean.java:158)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:85)
...

Based on the stack trace, I realized that since I am using Hibernate to version 3.3.x. In Hibernate 3.3.x (after some reading around) I came to know that they replaced apache commons-logging with slf4j maybe because of issues with commons-logging.

SLF4J requires you to use one of the binding jars. And you may need to download it from their website. Example: if you are using log4j, put slf4j-log4j12-.jar together with slf4j-api.jar.

Since I am using maven, I introduced the following lines in pom.xml

<dependency>
<groupid>org.slf4j</groupid>
<artifactid>slf4j-log4j12</artifactid>
<version>1.6.1</version>
</dependency>

This resolved the issue.

Comments

  1. I still cannot get StaticLoggerBinder to load.

    My error (I'm running Hibernate outside of Spring for now):

    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

    Even with the all the proper jars (I’m using log4j so I have slf4j-api-1.5.8.jar and slf4j-log4j12-1.5.8.jar in my classpath). I’ve googled this to death, and tried a trick with switching to an older version of slf4j (1.5.2 to be precise). Same error. I’ve tried Hibernate 3.3.2 and 3.6 getting the same results.

    I’ve looked in the jar and the class is there, but it just isn’t getting loaded. Any answers???

    ReplyDelete

Post a Comment

Popular posts from this blog

Hibernate: a different object with the same identifier value was already associated with the session

BeanDefinitionStoreException: Failed to parse configuration class: Could not find class [javax.jms.ConnectionFactory]