You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java 8迁移至Java 11遇package不可见构建错误求助

问题:Java 8迁移至Java 11时构建失败(com.sun.org.apache.xpath.internal.operations包不可见)

构建时出现如下错误:

package com.sun.org.apache.xpath.internal.operations is not visible
(package com.sun.org.apache.xpath.internal.operations is declared in module java.xml, which does not export it)

已尝试以下两种方案但均无效:

  • Stack Overflow相关问题的解决方案
  • Eclipse论坛的依赖排除方案,添加如下依赖排除后仍构建失败:
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.2</version>
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

可行修复方案

方案1:替换内部API为公开标准API(推荐)

com.sun.org.apache.xpath.internal.operations属于JDK私有内部API,Java 9引入模块化后不再对外暴露,长期来看必须替换为公开标准实现:

  1. 移除代码中对该内部包的导入语句
  2. 使用JDK标准javax.xml.xpath API重构代码,示例如下:
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

// 基础XPath查询示例
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//target/node";
String result = xpath.evaluate(expression, yourDocumentObject);

若需要更复杂的XPath特性,可引入Apache公开的Xerces库:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.12.2</version>
</dependency>

方案2:临时兼容配置(仅过渡使用)

如果暂时无法重构代码,可通过Maven编译参数强制JDK导出内部包:
在pom.xml中配置maven-compiler-plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
                <compilerArgs>
                    <arg>--add-exports</arg>
                    <arg>java.xml/com.sun.org.apache.xpath.internal.operations=ALL-UNNAMED</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

⚠️ 注意:此方案为临时妥协,内部API可能在后续JDK版本中被移除,不建议长期使用。

方案3:排查并解决依赖冲突

通过mvn dependency:tree命令查看项目依赖树,确认是否存在旧版xml-apis或冲突的XPath实现,针对性排除冲突依赖:

<dependency>
    <groupId>[冲突依赖的groupId]</groupId>
    <artifactId>[冲突依赖的artifactId]</artifactId>
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

内容的提问来源于stack exchange,提问作者user1971376

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 23:11:26