2

我们在代码中有以下情况,当 JAVA_HOME 设置为 JDK7 而不是 JDK6(源/目标 = 1.6 的 Maven)运行时导致测试失败

javax.xml.stream.FactoryConfigurationError: Provider for com.sun.xml.internal.stream.XMLInputFactoryImpl cannot be found
        at javax.xml.stream.XMLInputFactory.newFactory(XMLInputFactory.java:239)

代码本身

private static final String SUN_XML_INPUT_FACTORY = "com.sun.xml.internal.stream.XMLInputFactoryImpl";
private final XMLInputFactory xmlInputFactory;

public theConstructor() {
    // When deployed to WebLogic 10.3.5 it will use "weblogic.xml.stax.XMLStreamInputFactory" which will
    // cause a StreamReader exception as it requires some input stream other than null.
    xmlInputFactory = XMLInputFactory.newFactory(SUN_XML_INPUT_FACTORY, this.getClass().getClassLoader());
}

我想我应该使用其他东西,private static final String SUN_XML_INPUT_FACTORY = "com.sun.xml.internal.stream.XMLInputFactoryImpl";但我不知道是什么。

4

1 回答 1

1

我在JDK6之后发现,方法:

javax.xml.stream.XMLInputFactory.newFactory(String factoryId,ClassLoader classLoader)

改变它的程序。

该参数factoryId不是类路径,而是键。

所以我将一个stax.properties文件放入我的$java.home/jre/lib/文件中,上下文是:

com.bea.xml.stream.MXParserFactory=com.bea.xml.stream.MXParserFactory

然后我的问题解决了。

XMLInputFactory factory = XMLInputFactory.newFactory("com.bea.xml.stream.MXParserFactory", this.getClass().getClassLoader());
于 2016-03-29T11:12:51.540 回答