Search code examples
javaspringspring-bootapache-poi

java.lang.NoSuchMethodError: 'org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.poi-poi-ooxml-5.2.4


I have upgraded from org.apache.poi-poi-ooxml-5.2.3 to org.apache.poi-poi-ooxml-5.2.4 due to Security Violation Threat in 5.2.3

Now, I am facing run time exception as java.lang.NoSuchMethodError

Exception:

[ERROR] ErrorPageFilter - Forwarding to error page from request [/reports/myapp/myreport] due to exception ['org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.builder()']
java.lang.NoSuchMethodError: 'org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.builder()'
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.newPackage(XSSFWorkbook.java:521) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:231) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:227) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:215) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at myapp.reports.service.impl.MyReportsExcelExporter.<init>(MyReportsExcelExporter.java:37) ~[classes/:0.0.1-SNAPSHOT]

Code:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class MyReportsExcelExporter {
    protected XSSFWorkbook workbook;
    ...
    public MyReportsExcelExporter() {
        this.workbook = new XSSFWorkbook(); //Facing issue here, while initializing the workbook.
    }
    ...
}

Looking at the version change, it seems like a minor upgrade but now existing code has stopped working.

What's probably wrong?


Solution

  • You will need to add/upgrade Apache Commons IO dependency version >= 2.12.x.

    Note: The builder() method present in UnsynchronizedByteArrayOutputStream got introduced from 2.12.x version of commons-io onwards.

    I took the latest dependency of commons-io which is 2.14.0 at the time of writing the answer.

    pom.xml (Maven):

    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.14.0</version>
        </dependency>
    </dependencies>
    

    Or

    build.gradle (Gradle):

    dependencies {
       implementation 'commons-io:commons-io:2.14.0'
    }
    

    It will work.