Chris
Chris

Reputation: 3

Issue at loading word document with docx4j

I try to use docx4j to interact with word documents in my program (before I used Apache POI). But I meet an issue at the very beginning. I downloaded the last version of docx4j (11.4). Here is my code:

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);
    }

I have an issue at this line and I don't understand why: WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);

Here is the the error log with 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 [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232) at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189) at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at [email protected]/javafx.event.Event.fireEvent(Event.java:198) at [email protected]/javafx.scene.Node.fireEvent(Node.java:8875) at [email protected]/javafx.scene.control.Button.fire(Button.java:203) at [email protected]/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207) at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247) at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232) at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189) at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at [email protected]/javafx.event.Event.fireEvent(Event.java:198) at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3984) at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1890) at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2708) at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411) at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301) at java.base/java.security.AccessController.doPrivileged(AccessController.java:400) at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450) at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424) at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449) at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551) at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937) at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185) at java.base/java.lang.Thread.run(Thread.java:1583)

I searched on Google to find out why this happened, but I found nothing. No articles about that issue.

PS: Hope it's understandable

Upvotes: 0

Views: 526

Answers (2)

Chris
Chris

Reputation: 3

I finally found the solution. I post it here in the case of some may encounter same issues. It was as g00se said: a conflict between some jars.

To check those conflicts, I clicked on the maven tab at the right of Intellij IDEA window.

maven tab

Then, there is a small button "Analyze dependencies".

analyze dependencies

And from there, I solved all those conflicts and the app runs well.

Upvotes: 0

g00se
g00se

Reputation: 4296

It's mysterious. The following works perfectly well for me. All I did was get the latest (11.4.11) from Maven of docx4j-JAXB-ReferenceImpl and added it to my pom. See what happens when you run my code with your input file.

package com.technojeeves.docx4j;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.model.datastorage.migration.VariablePrepare;
import org.docx4j.openpackaging.exceptions.Docx4JException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.io.InputStream;
import java.io.OutputStream;

import java.nio.file.Files;
import java.nio.file.Path;

import jakarta.xml.bind.JAXBException; 

public class App {
    public static void main(String[] args) {
        String path = "input.docx";
        String outputPath = "output.docx";
        try {
            InputStream in = Files.newInputStream(Path.of(path));
            OutputStream out = Files.newOutputStream(Path.of(outputPath));
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(in);
            HashMap<String, String> mappings = new HashMap<>();
            VariablePrepare.prepare(wordMLPackage);
            mappings.put("<<<today>>>", DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.US).format(LocalDate.now()));
            mappings.put("%", "Hello");
            mappings.put("#", "Word");

            // Don't understand this yet
            wordMLPackage.getMainDocumentPart().variableReplace(mappings);
            wordMLPackage.getMainDocumentPart().addParagraphOfText("g00se is the greatest!");
            wordMLPackage.save(out);

        } 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);
        }
    }
}

Upvotes: 0

Related Questions