2014-10-02 278 views
2

我們在代碼中導致測試失敗時,將JAVA_HOME設置爲JDK7而不是JDK6(maven with source/target = 1.6)JDK6 - > JDK7無法使用com.sun.xml.internal.stream.XMLInputFactoryImpl

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";別的東西,但我不知道是什麼。

+0

我說如果在JAVA_HOME環境中設置了JDK 6,應用程序是否正常運行? – AntJavaDev 2014-10-02 10:55:09

+0

使用java內部類幾乎沒有好處。 「內部」的一點是,實施可能會在沒有通知的情況下發生變化。 – 2014-10-02 10:55:23

+0

查看「newFactory」的文檔,它可能會給你一個關於如何找到合理替換的想法。 http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html#newFactory() – 2014-10-02 10:59:24

回答

0

我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()); 
相關問題