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

Java 17下batik-transcoder与twelvemonkeys依赖冲突及类缺失问题

解决Java中TwelveMonkeys+Batik处理SVG的依赖冲突与类找不到问题

问题场景

在Java项目中引入TwelveMonkeys的imageio-batik和Apache Batik的batik-transcoder处理SVG文件时,遇到两个连续问题:

  1. 初始依赖引入后编译报错:

The package org.w3c.dom is accessible from more than one module: , java.xml
SAXException cannot be resolved to a type

  1. 手动排除xml-apis相关依赖后,编译通过但运行时抛出:
java.lang.ClassNotFoundException: org.w3c.dom.svg.SVGDocument

原因分析

  • 第一个错误是因为Batik依赖的xml-apis包与JDK自带的java.xml模块包含重复的org.w3c.dom基础类,触发模块路径冲突。
  • 第二个错误是因为直接排除所有xml-apis依赖后,丢失了Batik处理SVG必需的DOM扩展类(如org.w3c.dom.svg.SVGDocument),这些类并不在JDK默认的java.xml模块中。

解决方案

调整batik-transcoder的依赖排除规则,只排除与JDK冲突的基础xml-apis包,保留包含SVG扩展类的xml-apis-ext,具体依赖配置如下:

<!-- TwelveMonkeys SVG处理依赖 -->
<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-batik</artifactId>
    <version>3.9.4</version>
</dependency>

<!-- Batik转码器:仅排除冲突的基础xml-apis,保留SVG扩展类 -->
<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-transcoder</artifactId>
    <version>1.16</version>
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

如果上述配置后仍存在类找不到问题,可以显式引入xml-apis-ext依赖:

<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis-ext</artifactId>
    <version>1.3.04</version>
</dependency>

额外说明(JDK9+模块系统)

若项目使用JDK9+的模块化开发(存在module-info.java),需要在模块声明中添加以下依赖声明:

requires org.apache.batik.transcoder;
requires com.twelvemonkeys.imageio.batik;
requires java.xml;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 22:37:45