问题现象:
在Eclipse配置并编译worldwind java2.1.0源码,选中Src目录下gov.nasa.worldwindx.examples包下ApplicationTemplate.java类文件run时提示“javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V”异常。
解决办法:
1、定位到报错的方法为WWXML.java文件中的createDocumentBuilder方法:
public static DocumentBuilder createDocumentBuilder(boolean isNamespaceAware) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(isNamespaceAware); if (Configuration.getJavaVersion() >= 1.6) { try { docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e) { // Note it and continue on. Some Java5 parsers don‘t support the feature. String message = Logging.getMessage("XML.NonvalidatingNotSupported"); Logging.logger().finest(message); } } try { return docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { String message = Logging.getMessage("XML.ParserConfigurationException"); Logging.logger().finest(message); throw new WWRuntimeException(e); } }
2、在该方法中标注黄色的一句代码DocumentBuilderFactory.newInstance()前面使用前加入如下一行代码:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
3、完整代码如下:
public static DocumentBuilder createDocumentBuilder(boolean isNamespaceAware) { System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(isNamespaceAware); if (Configuration.getJavaVersion() >= 1.6) { try { docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e) { // Note it and continue on. Some Java5 parsers don‘t support the feature. String message = Logging.getMessage("XML.NonvalidatingNotSupported"); Logging.logger().finest(message); } } try { return docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { String message = Logging.getMessage("XML.ParserConfigurationException"); Logging.logger().finest(message); throw new WWRuntimeException(e); } }
参考链接:
1、解决javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V异常