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

使用docx4j 11.4加载Word文档时出现NoSuchMethodError问题求助

使用docx4j 11.4加载Word文档时触发NoSuchMethodError

我尝试用docx4j 11.4替代Apache POI处理Word文档,在加载文件阶段就报错,代码如下:

String path = "input.docx";
File file = new File(path);
try {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);
    HashMap<String, String> mappings = new HashMap<>();
    VariablePrepare.prepare(wordMLPackage);
    mappings.put("<<<today>>>", new SimpleDateFormat("d MMMM yyyy").format(new Date()));
    mappings.put("%", "Hello");
    mappings.put("#", "Word");
             
    wordMLPackage.getMainDocumentPart().variableReplace(mappings);
    wordMLPackage.save("output.docx");
    
} catch (Docx4JException e) {
    System.out.println("Docx4JException");
    throw new RuntimeException(e);
} catch (JAXBException e) {
    System.out.println("JAXBException");
    throw new RuntimeException(e);
} catch (Exception e) {
    System.out.println("Exception");
    throw new RuntimeException(e);
}

错误出在这一行:

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);

IntelliJ IDEA中的错误日志:

Exception in thread "JavaFX Application Thread" java.lang.NoSuchMethodError: 'org.apache.commons.compress.archivers.zip.ZipArchiveEntry org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.getNextEntry()'
at org.docx4j.openpackaging.io3.stores.ZipPartStore.(ZipPartStore.java:141)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:570)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:421)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:298)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:276)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:170)
at fr.analyse.assistant.model.Word.printingLetters(Word.java:307)
at fr.analyse.assistant.controllers.VController.letter(VController.java:470)
at fr.analyse.assistant.controllers.VController.lambda$initialize$6(VController.java:215)
at javafx.base@21.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base@21.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base@21.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at javafx.base@21.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@21.0.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base@21.0.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@21.0.1/javafx.scene.Node.fireEvent(Node.java:8875)
at javafx.controls@21.0.1/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls@21.0.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
at javafx.controls@21.0.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base@21.0.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base@21.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base@21.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base@21.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at javafx.base@21.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@21.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@21.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@21.0.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base@21.0.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@21.0.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3984)
at javafx.graphics@21.0.1/javafx.scene.Scene.processMouseEvent(Scene.java:1890)
at javafx.graphics@21.0.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2708)
at javafx.graphics@21.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics@21.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@21.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics@21.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics@21.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics@21.0.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics@21.0.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics@21.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@21.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
at java.base/java.lang.Thread.run(Thread.java:1583)

我谷歌搜索后没找到相关解决办法,求帮忙。


解决办法

这个错误是commons-compress版本不兼容导致的:docx4j 11.4调用了ZipArchiveInputStream.getNextEntry()方法(返回类型为ZipArchiveEntry),但你的项目中引入的commons-compress版本过旧,没有这个方法(旧版本的getNextEntry()返回的是ZipEntry,而非ZipArchiveEntry)。

具体修复步骤:

  1. 检查依赖冲突:在IntelliJ中打开Maven Projects面板,执行Dependency Analyzer,搜索commons-compress,查看是否有多个版本,尤其是旧版本(比如1.23.x及以下)。
  2. 排除旧版本依赖:如果是其他依赖(比如Apache POI)引入了旧版commons-compress,在pom.xml中排除:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>xxx</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  1. 引入兼容版本:添加docx4j 11.4要求的commons-compress版本(建议用1.24.0或更高,可查看docx4j官方pom确认):
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.24.0</version>
</dependency>
  1. 刷新依赖并重启项目:执行Maven -> Reload All Projects,然后重启IntelliJ并重新运行程序。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 12:25:55