libero
libero

Reputation: 201

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

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.

Upvotes: 18

Views: 75699

Answers (11)

Axel
Axel

Reputation: 151

Changed all dependencies to last version and it works for me:

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

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.4.0</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.18.0</version>
</dependency>

Upvotes: 0

MaduKan
MaduKan

Reputation: 670

I landed here because of the following error

NoClassDefFoundError org/apache/commons/io/function/Uncheck

Apparently the root cause is the same. Some dependancies have changed (in my case, for Apache CSV, on an integration with Spring Shell, if that's important..) (Note that the solution below is guided by some suggestions on this thread.)

Solution was to change from Apache Commons IO from 2.11.0 to 2.16.1:

Hope this helps somebody.

In case, if someone is interested on the full list of library changes, they are as follows:

Before:

    <properties>
        <spring-shell.version>3.2.4</spring-shell.version>
        <apache-commons-io.version>2.11.0</apache-commons-io.version>
        <apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
        <apache-commons-text.version>1.12.0</apache-commons-text.version>
        <apache-commons-logging.version>1.3.1</apache-commons-logging.version>
        <apache-commons-collections4.version>4.4</apache-commons-collections4.version>
        <apache-commons-csv.version>1.11.0</apache-commons-csv.version>
    </properties>

After:

    <properties>
        <spring-shell.version>3.2.4</spring-shell.version>
        <apache-commons-io.version>2.16.1</apache-commons-io.version>
        <apache-commons-lang3.version>3.14.0</apache-commons-lang3.version>
        <apache-commons-text.version>1.12.0</apache-commons-text.version>
        <apache-commons-logging.version>1.3.1</apache-commons-logging.version>
        <apache-commons-collections4.version>4.5.0-M1</apache-commons-collections4.version>
        <apache-commons-csv.version>1.11.0</apache-commons-csv.version>
    </properties>

Upvotes: 0

Balbino Gachuz
Balbino Gachuz

Reputation: 21

Works for me:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.3</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>

Upvotes: 1

Prakash Manikandan
Prakash Manikandan

Reputation: 11

This issue is resolved with following dependencies: [Dependencies]

Upvotes: 0

Kiran Antony
Kiran Antony

Reputation: 240

Looks like you don't have commons-io dependency. please add this dependency

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

Upvotes: 4

Jeffrey Wang
Jeffrey Wang

Reputation: 91

I recently upgraded Apache POI to 5.2.3 and was having the same ClassNotFoundException. Since the application was working fine before the upgrade, so I quickly did three things to debug:

  1. Search on stackoverflow.com ;-)
  2. Check to see from what version of Apache Commons IO has the org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream class. In the JavaDoc, it was since 2.7.
  3. Check my maven dependency tree to find out what version of the Apache Commons IO I am using. Turns out the commons-io I had was 2.2 and it was from the commons-fileupload 1.4 in my maven pom.xml

Once I added the latest available Apache Commons IO 2.11.0 to my maven pom.xml and rebuild the application, the problem solved. I hope this help!

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

Upvotes: 9

neel jain
neel jain

Reputation: 33

It worked for me by removing this dependency

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

Upvotes: 1

Luiz Henrique Serafim
Luiz Henrique Serafim

Reputation: 181

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>

Upvotes: 18

Winky
Winky

Reputation: 21

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

Upvotes: 2

Anon
Anon

Reputation: 57

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.

Upvotes: 4

Saurabh Jhunjhunwala
Saurabh Jhunjhunwala

Reputation: 2922

Add

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

to you pom

Upvotes: 6

Related Questions