javax.xml.parsers.SAXParserFactory ClassCastException
当通过 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
经过谷歌搜索后,我发现了几页描述其背后的主要问题(不同类加载器中的几个 SAXParserFactoryImpl)。
-> http://www.xinotes.org/notes/note/702/
我的问题是,我如何找出哪个库也提供 SAXParserFactoryImpl,以便我可以排除它。我正在使用 Maven、IntelliJ 和 JDK 1.6.0_23。该问题发生在命令行以及从 IntelliJ 运行测试时。
但奇怪的问题是,在构建服务器上不会发生此问题。
更新 1
刚刚发现当我在 mvn clean 之后第一次运行 mvn test 时,错误不会出现。但是一旦我再次运行 mvn test (没有 clean,就会发生异常)(当我从 IntelliJ 运行它时)。
当我在 cmd 行上运行它时,几个 mvn 测试调用就会起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了这个问题。它与尝试加载 SAXParserFactory 的 PowerMockito 有关。我没有弄清楚这一点的原因是因为堆栈跟踪仅包含两次 PowerMockito,并且这位于中间:-)
因此,如果您在 IntelliJ 中解决了这个问题并且您确实使用了 PowerMockito,请使用以下内容注释您的测试类注释:
这已经解决了我的问题。
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:
This has solved the problem in my case.
您的 JDK 可能有自己的 SAXParserFactoryImpl。
检查 xercesImpl、xml/xml-api 和 sax 等 jar 文件。
您的一台服务器可能会使用来自服务器的一台。
您可以使用 jarfinder: http://www.jarfinder.com/index .php/java/search/~SAXParserFactoryImpl~
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~
我今天遇到了同样的错误。经过大量挖掘后,我发现这里或其他地方的解决方案没有帮助。
然而,经过一番尝试后,我发现了一个确定性的解决方案,这与接受的答案不同,它并不适用于所有情况。
答案是,通过堆栈跟踪查找任何 ClassCast 异常,然后将它们添加到 \@PowerMockIgnore 列表中。不断重复,直到问题解决。对我来说就像魔术一样。
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.