javax.xml.parsers.SAXParserFactory ClassCastException

发布于 2024-12-16 20:35:20 字数 840 浏览 0 评论 0原文

当通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花心好男孩 2024-12-23 20:35:20

我发现了这个问题。它与尝试加载 SAXParserFactory 的 PowerMockito 有关。我没有弄清楚这一点的原因是因为堆栈跟踪仅包含两次 PowerMockito,并且这位于中间:-)

因此,如果您在 IntelliJ 中解决了这个问题并且您确实使用了 PowerMockito,请使用以下内容注释您的测试类注释:

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

这已经解决了我的问题。

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.

带上头具痛哭 2024-12-23 20:35:20

您的 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~

尽揽少女心 2024-12-23 20:35:20

我今天遇到了同样的错误。经过大量挖掘后,我发现这里或其他地方的解决方案没有帮助。

然而,经过一番尝试后,我发现了一个确定性的解决方案,这与接受的答案不同,它并不适用于所有情况。

答案是,通过堆栈跟踪查找任何 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文