Hibernate and JUnit-problem: LogConfigurationException

Hi,


I wanted to write a Testclass in JUnit with eclipse, which at first loads something out of my database with Hibernate.
But I get the following error:

java.lang.ExceptionInInitializerError
     at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:51)
     at dao.BaseDAO.saveOrUpdateObj(BaseDAO.java:34)
     at dao.RechtsformDAO.saveOrUpdate(RechtsformDAO.java:31)
     at test.src.common.business.GegnerTest.setUp(GegnerTest.java:86)
     at junit.framework.TestCase.runBare(TestCase.java:125)
     at junit.framework.TestResult$1.protect(TestResult.java:106)
     at junit.framework.TestResult.runProtected(TestResult.java:124)
     at junit.framework.TestResult.run(TestResult.java:109)
     at junit.framework.TestCase.run(TestCase.java:118)
     at junit.framework.TestSuite.runTest(TestSuite.java:208)
     at junit.framework.TestSuite.run(TestSuite.java:203)
     at junit.framework.TestSuite.runTest(TestSuite.java:208)
     at junit.framework.TestSuite.run(TestSuite.java:203)
     at junit.swingui.TestRunner$16.run(TestRunner.java:623)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.) (Caused by org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.))
     at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
     at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
     at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
     at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
     at net.sf.hibernate.cfg.Configuration.<clinit>(Configuration.java:95)
     ... 14 more
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.)
     at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
     at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
     ... 18 more
Caused by: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.
     at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:385)
     ... 19 more



Does anyone know, which properties (and where) I have to change to get rid of that effect?

Thanks!!
spetrowitschAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Dejan PažinConnect With a Mentor Head of SW DevelopmentCommented:


OK, I think I now what the problem is.

The cause is in the class loaders. The Log  implementation is loaded through different classloader than the Log interface and are therefore considered different, which is why you get the exception.

To really understand this you should read this: http://www.qos.ch/logging/classloader.jsp

It explains the problem in detail and there are examples that show you what causes the problem.
0
 
Dejan PažinHead of SW DevelopmentCommented:


You should check all the jar files that are used in your project.

There seem to be more than one jar file containing org.apache.commons.logging.Log class of various versions. Try to remove all but one.
0
 
Dejan PažinHead of SW DevelopmentCommented:


The jar file containing org.apache.commons.logging.Log class is most probably named commons-logging-1.x.x.jar.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
spetrowitschAuthor Commented:
dejanpazin, I have that commons-loggin-1.0.4.jar
But when I remove it from my jars, I get another error:

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
      at net.sf.hibernate.cfg.Configuration.<clinit>(Configuration.java:95)
      at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:51)
      at src.common.businesstk.Vorsatz.vorsatzSpeichern(Vorsatz.java:63)
      at src.common.schnittstellen.tk.TkFaktPureImport.addInfosatz(TkFaktPureImport.java:83)
      at test.src.common.schnittstellen.tk.TkSchnittstelleFaktPure.testAddInfosatz(TkSchnittstelleFaktPure.java:135)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at junit.framework.TestCase.runTest(TestCase.java:154)
      at junit.framework.TestCase.runBare(TestCase.java:127)
      at junit.framework.TestResult$1.protect(TestResult.java:106)
      at junit.framework.TestResult.runProtected(TestResult.java:124)
      at junit.framework.TestResult.run(TestResult.java:109)
      at junit.framework.TestCase.run(TestCase.java:118)
      at junit.framework.TestSuite.runTest(TestSuite.java:208)
      at junit.framework.TestSuite.run(TestSuite.java:203)
      at junit.framework.TestSuite.runTest(TestSuite.java:208)
      at junit.framework.TestSuite.run(TestSuite.java:203)
      at junit.swingui.TestRunner$16.run(TestRunner.java:623)


For me it seems, that I have to tell hibernate not to log at all - but I have no idea where...
0
 
Dejan PažinHead of SW DevelopmentCommented:

From the exception it looks like you have org.apache.commons.logging.Log class in more jars. Try to do the following:

1. Check the classpath of your application and see all the jar files that it uses.
2. Search all those jars for org.apache.commons.logging.Log class
3. Dont remove  commons-loggin-1.0.4.jar, cause you need that one, remove any other jars that contain org.apache.commons.logging.Log class

0
 
spetrowitschAuthor Commented:
Thanks, dejanpazin, I searched every jar-file, but there is still no other jar-file containing org.apache.commons.logging.Log-class
0
 
Dejan PažinHead of SW DevelopmentCommented:

The only other idea I can think of is that you remove the org.apache.commons.logging.Log class from the commons-loggin-1.0.4.jar nad try running the application.

If the Log class really doesnt occur anywhere else, you should get error: java.lang.NoClassDefFoundError: org/apache/commons/logging/Log

0
 
spetrowitschAuthor Commented:
I removed the Log.class from the jar-file.

I get the message:
java.lang.ExceptionInInitializerError
      at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:51)
        ...
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.ClassNotFoundException: org.apache.commons.logging.Log (Caused by java.lang.ClassNotFoundException: org.apache.commons.logging.Log) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.ClassNotFoundException: org.apache.commons.logging.Log (Caused by java.lang.ClassNotFoundException: org.apache.commons.logging.Log))
      at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
      at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
      at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
      at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
      at net.sf.hibernate.cfg.Configuration.<clinit>(Configuration.java:95)
      ... 19 more


Line 51 in HibernateUtil is:
Configuration cfg = new Configuration();
0
 
spetrowitschAuthor Commented:
@dejanpazin

Great, I see, that´s really the problem I encounter. I´ll see, whether I´m able to fix it...
0
 
spetrowitschAuthor Commented:
@dejanpazin

Now I know, where the error is - but I don´t know, how to solve it... I think, Hibernate and JUnit are both using the same logger, but in both cases I don´t use the class-loader, but it´s somewhere inside?!? So I don´t know, how to tell them to solve that parent-child-problem with ClassLoader...

Can you give me any hint?
0
 
Dejan PažinHead of SW DevelopmentCommented:

Hint is best I can do at this time. I'd try the following:

1. Try to see which classloader loads the classes first. Use

Thread.currentThread().getContextClassLoader();

I guess that would be in your case just before you call

Configuration cfg = new Configuration(); (its where the error occurs if you remove the Log class)

2. Set the same classloader at a place where error occurs (not the NoClassFound exception, but the exception of classloaders), use

Thread.currentThread().setContextClassLoader();

I belive this is where you have that:

GegnerTest.setUp()


0
 
aozarovCommented:
You can try to call (before Configuration cfg = new Configuration()):
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
If this doesn't work by itself then you can try also to put commons-loggin-1.0.4.jar
under your JVM (that one that is running eclipse)  jre\lib\ext folder
0
 
spetrowitschAuthor Commented:
@aozarov

Sorry, that doesn´t work.
0
 
aozarovCommented:
Did you try them both (set context class loader as well as making sure that only one instance of commons-loggin exists and it is under jre\lib\ext)?
If so, do you get the same problem or do you get any other exception?
0
 
spetrowitschAuthor Commented:
It may sound unbelievable, but now I get the message:

(Could not find the main-class)

java.lang.NoClassDefFoundError: test/app/LexTest
Exception in thread "main"

0
 
spetrowitschAuthor Commented:
I use not JUnit-module-Tests, not with GUI and TestSuites - and it works. Thanks to both of you!
0
All Courses

From novice to tech pro — start learning today.