javax.xml.parsers.SAXParserFactory ClassCastException
Asked Answered
P

3

16

I get on my local machine the following exception when running the tests by maven (mvn test).

ch.qos.logback.core.joran.event.SaxEventRecorder@195ed659 - Parser configuration error occured 
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory

After googling around I came across several pages which describe the main problem behind it (several SAXParserFactoryImpl in different classloaders).

-> http://www.xinotes.org/notes/note/702/

My question is, how can I figure out which library is also providing the SAXParserFactoryImpl, so that I can exclude it. I am using Maven, IntelliJ and JDK 1.6.0_23. The issue occurs on the command line as well as when running the tests from IntelliJ.

But the strange issue is, that on the build server this issue doesn't occur.

Update 1

Just figured out when I run the first time mvn test after an mvn clean, the error doesn't appear. But as soon as I run mvn test again (without clean, the exception occurs) (when I run it from IntelliJ).

When I run it on the cmd line, then several mvn test calls do work.

Phototonus answered 18/11, 2011 at 8:2 Comment(3)
can you specify which jars you are using so that i come to know which makes conflict?Phelips
I uploaded the maven pom.xml to heypasteit.com/clip/039L.Phototonus
Looking at your dependencies its probably an indirect dependency.Immiscible
P
43

I found the issue. It was related to PowerMockito who tried to load the SAXParserFactory. The reason why I haven't figured that one out was because the stacktrace contained only twice PowerMockito, and this at the middle :-)

So If you figure out this problem in IntelliJ and you do use PowerMockito, annotate your test class with the following annotation:

@PowerMockIgnore(["javax.management.*", "javax.xml.parsers.*",
         "com.sun.org.apache.xerces.internal.jaxp.*", "ch.qos.logback.*", "org.slf4j.*"])

This has solved the problem in my case.

Phototonus answered 21/12, 2011 at 13:1 Comment(3)
For future readers: when this happens due to logging, then (recommended) using @MockPolicy(Slf4jMockPolicy.class) or @MockPolicy(Log4jMockPolicy.class), or otherwise excluding the logging framework with @PowerMockIgnore("org.apache.log4j.*") or @PowerMockIgnore("org.apache.commons.logging.*") might help. See the PowerMock FAQ.Rachitis
This answer didn't work for me, actually it made the problem worse, after adding this annotation, my tests failed with the incoherent error com.sun.org.apache.xerces.internal.impl.dv.DVFactoryException: DTD factory class com.sun.org.apache.xerces.internal.impl.dv.dtd.DTDDVFactoryImpl does not extend from DTDDVFactory.Thermometer
I should have known. PowerMock ruins everything.Fabriane
I
1

Your JDK probably has its own SAXParserFactoryImpl.

Check for jars like xercesImpl, xml/xml-api and sax.

One your server the one from the server is probably going to be used.

You can use a jarfinder: http://www.jarfinder.com/index.php/java/search/~SAXParserFactoryImpl~

Immiscible answered 18/11, 2011 at 8:8 Comment(0)
M
0

I encountered the same error today. After a lot of digging, I found that the solutions here, or on other places are not helpful.

However, after playing around, I found a solution that works deterministically, unlike the accepted answer which does not apply to all cases.

The answer is, look through stack trace to find any ClassCast exceptions, and just add them to \@PowerMockIgnore list. Keep repeating until the issue is solved. Worked like magic for me.

Michaelmichaela answered 5/7, 2018 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.