问题描述
我在切换到 Java 11 后遇到 com.sun.org.apache.xerces.internal.dom#DOMInputImpl() 问题。在这段代码中:
@Override
public LSInput resolveResource(final String type,final String namespaceURI,final String publicId,String systemId,final String baseURI) {
final LSInput input = new DOMInputImpl();
if (systemId.equals(this.systemId)) {
try {
input.setByteStream(xsdFile.getInputStream());
} catch (IOException e) {
log.error("Error to get classpath resource for schema");
}
}
return input;
}
编译时出现错误:
错误:(8,42) java: 包 com.sun.org.apache.xerces.internal.dom 不可见
我尝试添加到 maven-surefire-plugin --add-exports...但它没有帮助我
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<argLine>--illegal-access=permit</argLine>
<argLine>--add-exports java.xml/com.sun.org.apache.xerces.internal.dom=ALL-UNNAMED</argLine>
<argLine>-Dillegal-access=permit</argLine>
</configuration>
</plugin>
解决方法
您应该使用原始的 xerces 实现。
这里也回答了这个问题:https://github.com/adoptium/adoptium-support/issues/199#issuecomment-717008260
在 JDK 9 中,引入了 Java 平台模块系统。作为其中的一部分,对最初不应该使用的内部 API 的访问被关闭。解决方案:升级您的依赖项,停止使用内部 API。对于 Xerces,库可从 https://xerces.apache.org/ 获得。