java – Apache POI upgrade to 5.0 error for Word document

We are upgrading Apache poi library from 3.17 to 5.0 version, when we update poi 5.2 dependency in POM.XML, getting the below mentioned error, while creating the document from template.

Code to create document

String relativeUrl = "/report-templates/DocTemplate.docx";
        XWPFDocument wordDoc=new XWPFDocument(getClass().getClassLoader().getResourceAsStream(relativeUrl));
    wordDoc.enforceUpdateFields();

Error Message

"Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: 'org.apache.xmlbeans.XmlObject[] org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTFootnotesImpl.getXmlObjectArray(javax.xml.namespace.QName, org.apache.xmlbeans.XmlObject[])'",

Dependency used for POI

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>

enter image description here

We tried different ways, like adding the latest dependencies of all related jars explicitly like xmlbean, poi-ooxml-schema, commons collections jars etc, but still not working.

Adding POM.xml for reference below

 <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.17.1</version>
        </dependency>
        
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.17.1</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.17.1</version>
            </dependency> 
        
        <dependency>
            <groupId>org.jasypt</groupId>
            <artifactId>jasypt-spring31</artifactId>
            <version>1.9.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
            <scope>compile</scope>
        </dependency>


        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.4</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>

        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>



        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>
<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
                        
        <dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

Full stack trace:

    2022-06-06 07:40:38 ERROR stderr:71 - java.lang.NoSuchMethodError: 'org.apache.xmlbeans.XmlObject[] org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTFootnotesImpl.getXmlObjectArray(javax.xml.namespace.QName, org.apache.xmlbeans.XmlObject[])'

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTFootnotesImpl.getFootnoteArray(CTFootnotesImpl.java:52)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.apache.poi.xwpf.usermodel.XWPFFootnotes.onDocumentRead(XWPFFootnotes.java:85)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.apache.poi.xwpf.usermodel.XWPFDocument.initFootnotes(XWPFDocument.java:262)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:155)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:169)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:119)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//com.example.test.service.impl.testReportServiceImpl.createWordReport(testReportServiceImpl.java:890)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//com.example.test.service.impl.testReportServiceImpl.generateWordReport(testReportServiceImpl.java:821)

2022-06-06 07:40:38 ERROR stderr:71 -   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2022-06-06 07:40:38 ERROR stderr:71 -   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

2022-06-06 07:40:38 ERROR stderr:71 -   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2022-06-06 07:40:38 ERROR stderr:71 -   at java.base/java.lang.reflect.Method.invoke(Method.java:566)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)

2022-06-06 07:40:38 ERROR stderr:71 -   at deployment.test-web.war//org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)

Leave a Comment