Tuesday, June 15, 2010

ehcache relies on slf4j

I couldn't get my unit tests to run from my Ant task after switching to ehcache for Hibernate. Every test would fail with the following error:
java.lang.NoClassDefFoundError: Could not initialize class net.sf.ehcache.hibernate.EhCacheProvider
I followed the instructions in this blog entry to pretty print the classpath and confirm that ehcache.jar was indeed on the Junit test classpath. The application runs fine inside the app server, so what gives?

I solved the mystery by modifying my test so that it only ran one line of code:
System.out.println(Class.forName("net.sf.ehcache.hibernate.EhCacheProvider").newInstance());
This gave me a more useful error message:
java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.LoggerFactory
Aha! I didn't have jboss-home/common/lib/sfl4j-api.jar on my classpath. I added it and one other slf4j jar to my classpath and my problem was solved.

There goes two hours due to a misleading error message.

No comments:

Post a Comment