java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream on using HSSFWorkbook

Viewed 7474

I'm trying to implement reading from .xls file. I have the following code:

FileInputStream file = null;
    Workbook workbook = null;
    try {
        file = new FileInputStream(System.getProperty("user.home") + "/Downloads/" + fileName);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    if (fileName.endsWith(".xls")) {
        try {
            **workbook = new HSSFWorkbook(file);**
        } catch (IOException e) {
            e.printStackTrace();
        }

This marked code line crashes.

I imported in pom.xml:

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

but I got the error: Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream

I would appreciate help on this. Thanks.

4 Answers

Add

<dependency>
    <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
    <version>1.4</version>
  </dependency>

to you pom

Change the dependency to

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

I used the version 5.2.0 as well and was getting the same error and changing that fixed it.

When I read excel to XSSFWorkbook, it prompt error message that "commons.io.output.UnsynchronizedByteArrayOutputStream" finally fixed it by downgrad the version of poi from 5.2.3 to 5.2.2

It worked for me

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>

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

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