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

JDK11迁移JAXB遇ContextFactory找不到问题求助

JDK8迁移至JDK11的JAXB加载问题解决方案

针对你遇到的javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath问题,结合你的Spring Boot 2.6.7 + JDK11环境,给出以下针对性解决方案:

1. 使用Spring Boot官方JAXB Starter简化依赖管理

Spring Boot 2.x提供了专门的JAXB starter,能自动管理所有相关依赖的版本,避免手动组合依赖导致的冲突。直接在pom.xml中添加:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jaxb</artifactId>
</dependency>

如果不需要starter,也可以手动引入版本统一的依赖组合(和Spring Boot 2.6.7兼容的版本):

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.3</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.3</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

2. 对齐maven-jaxb23-plugin的JAXB版本

确保插件使用的JAXB版本和项目依赖一致,避免插件引入的依赖覆盖项目配置。修改插件配置:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb23-plugin</artifactId>
    <version>0.15.3</version>
    <configuration>
        <jaxbVersion>2.3.3</jaxbVersion>
        <!-- 保留你的其他插件配置 -->
    </configuration>
    <dependencies>
        <!-- 显式指定插件使用的XJC版本,避免版本冲突 -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.3.3</version>
        </dependency>
    </dependencies>
</plugin>

3. 排查并解决依赖冲突

使用Maven命令生成依赖树,检查是否有其他依赖引入了旧版本或不兼容的JAXB包:

mvn dependency:tree | grep jaxb

如果发现冲突(比如旧版本的jaxb-api),在对应的依赖中排除冲突项:

<dependency>
    <!-- 存在冲突的依赖 -->
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

4. 正确设置ContextFactory系统属性

不要在代码中设置系统属性(时机太晚,可能JAXB已经初始化),改用VM启动参数或配置文件:

  • IDE启动配置添加VM参数:
    -Djavax.xml.bind.JAXBContextFactory=com.sun.xml.bind.v2.ContextFactory
    
  • 或在application.properties中添加:
    javax.xml.bind.JAXBContextFactory=com.sun.xml.bind.v2.ContextFactory
    

5. 验证类加载路径

添加VM参数打印类加载日志,确认com.sun.xml.bind.v2.ContextFactory是否被正确加载:

-verbose:class

启动后搜索该类名:

  • 如果无加载记录:说明jaxb-impl包不在类路径中,检查Maven依赖是否正确导入,IDE是否同步了依赖。
  • 如果有加载记录但仍报错:可能存在类加载器隔离问题(比如第三方框架的类加载器优先加载了其他版本的类),需要排查框架的类加载配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 20:40:58