Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate in Servlet causesNoClassDefFoundError: org/slf4j/LoggerFactory

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

I then created a GWT (V1.7) Servlet web app, copied my hibernate classes into it, and added the same User Libraries dependencies. However when i run the servlet and try to access a URL which invokes my code i get this:

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 is :

AnnotationConfiguration config = new AnnotationConfiguration ();

Googling for this error suggests i am missing slf4j-api.jar from the classpath, however if i look at the command line for the Debug properties i can 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 can see the LoggerFactory class in there.

Any idea why it isn't being found by the class loader?

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

Edit 2: If i add a Test class with a main which calls the same code and run it, it works. So this classpath problem seems specific to the Servlet.

thanks, jon

like image 580
jon-hanson Avatar asked Dec 23 '22 08:12

jon-hanson


1 Answers

It seems that copying the two slf4j jars to the war/WEB-INF/lib sub-project/dir fixed the problem. I'm not really sure why i should need to do this for these two jars and not for all the other Hibernate, Restlet etc jars that the project also uses, though i suppose for consistency i will do that anyway - i guess it will make deployment easier as well.

If someone can provide some sort of explanation as to why this worked and why exactly i need to do it i will select it as the "correct" answer, otherwise i'll select this one.

like image 195
jon-hanson Avatar answered Jan 25 '23 23:01

jon-hanson