Hibernate in Servlet raises NoClassDefFoundError: org / slf4j / LoggerFactory

I have a Java (6) application that uses Hibernate (V3.3.2) to read data from HSQLDB, which I collect and debug / run using Eclipse (V3.5.1), and it works fine.

Then I created the GWT servlet web application (V1.7), copied my hibernation classes into it, and added the same user library dependencies. However, when I run the servlet and try to access the url that calls my code, I get the following:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152) at xxx.daoimpl.DAOSession.initialise(DAOSession.java:40) 

where DAOSession.java:40:

 AnnotationConfiguration config = new AnnotationConfiguration (); 

Googling for this error assumes that I skip slf4j-api.jar from the class path, however, if I look at the command line for Debug properties, I see this jar there:

 C:\java\jsedk_6\jre\bin\javaw.exe -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:54541 -Xmx512m -Dfile.encoding=Cp1252 -classpath D:\dev\workspace\xxx\src; D:\dev\workspace\xxx\resources; D:\dev\workspace\xxx\war\WEB-INF\classes; C:\java\eclipse\plugins\com.google.gwt.eclipse.sdkbundle.win32_1.7.1.v200909221731\gwt-windows-1.7.1\gwt-user.jar; C:\java\eclipse\plugins\com.google.gwt.eclipse.sdkbundle.win32_1.7.1.v200909221731\gwt-windows-1.7.1\gwt-dev-windows.jar; C:\java\hibernate-annotations-3.4.0.GA\hibernate-annotations.jar; C:\java\hibernate-annotations-3.4.0.GA\lib\ejb3-persistence.jar; C:\java\hibernate-annotations-3.4.0.GA\lib\hibernate-commons-annotations.jar; C:\java\hibernate-distribution-3.3.2.GA\hibernate3.jar; C:\java\hibernate-distribution-3.3.2.GA\lib\required\antlr-2.7.6.jar; C:\java\hibernate-distribution-3.3.2.GA\lib\required\commons-collections-3.1.jar; C:\java\hibernate-distribution-3.3.2.GA\lib\required\dom4j-1.6.1.jar; C:\java\hibernate-distribution-3.3.2.GA\lib\required\javassist-3.9.0.GA.jar; C:\java\hibernate-distribution-3.3.2.GA\lib\required\jta-1.1.jar; C:\java\hibernate-validator-4.0.1.GA\hibernate-validator-4.0.1.GA.jar; C:\java\hibernate-validator-4.0.1.GA\lib\validation-api-1.0.0.GA.jar; C:\java\hibernate-validator-4.0.1.GA\lib\log4j-1.2.14.jar; C:\java\hsqldb\lib\hsqldb.jar; C:\java\restlet-jse-2.0m5\lib\org.restlet.jar; C:\java\restlet-jee-2.0m5\lib\org.restlet.ext.servlet.jar; C:\java\restlet-jse-2.0m5\lib\org.restlet.ext.xml.jar; C:\java\slf4j-1.5.8\slf4j-api-1.5.8.jar; C:\java\slf4j-1.5.8\slf4j-log4j12-1.5.8.jar com.google.gwt.dev.HostedMode ... 

If I open the jar, I see the LoggerFactory class there.

Any idea why it was not found by the class loader?

Edit 1: If you try to access org.slf4j.LoggerFactory from my code, Eclipse compiles it in order, but I get the same error at runtime.

Edit 2: If I add a test class with a main one that calls the same code and runs it, it works. So this problem with classpath seems to be servlet specific.

Thanks, John

+4
source share
4 answers

It seems that copying two slf4j flags to the war / WEB-INF / lib routine has been fixed. I'm not quite sure why I need to do this for these two cans, and not for all the other bars of Hibernate, Restlet, etc., which also use the project, although I assume that for consistency I will do it anyway - I think This will make deployment easier as well.

If someone can give some explanation of why this worked and why I need to do it, I will choose it as the “correct” answer, otherwise I will choose this one.

+6
source

Can you confirm that you have at least two slf4j.jar files in the classpath, slf4j-api.jar and exactly one implementation, for example slf4j-jdk14.jar?

You should not have multiple sflj4 implementations in the classpath.

+3
source

You have a problem with a dependency on execution, so everything compiles fine, but the dependency is in your banks. You need to visit the Hibernate website and look at the compatibility of matrix and make sure that you are configured correctly, and then check the annotation and kernel dependencies. Your log4J traps look great, so this is definitely some kind of quirk.

If it works in Eclipse, then logically definitely there is some difference between the two versions of time (eclipse and non-eclipse), if the matrix checks perfectly, then see if you can divide the similarities into 2, and then figure out the differences - this is your answer must be there.

+1
source

I had a similar problem, except that I used Tomcat and the NoClassDefFound error was in juli logger. I fixed this by removing Tomcat dependencies from my class path when working in host mode, because the hosting mode built in a Tomcat server that was conflicting. So I would see what happens if you remove some or all of the restart bans from your class path to make sure they conflict.

+1
source

All Articles