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

10

15

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.

Eldwen answered 18/3, 2022 at 16:20 Comment(0)
B
15

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>
Bipartite answered 23/9, 2022 at 14:20 Comment(2)
So I must not understand something about Maven and dependencies. How as POI package built without this dependency in its pom.xml?Crude
as David mentions: I had only the poi-ooxml dependency when working with Java17. After upgrading to Java21, I also needed to add the commons-io 2.15.0 in order to prevent OP's mentioned exception when generating a new XSSFWorkBookHeller
K
9

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>
Kenna answered 18/1, 2023 at 17:36 Comment(0)
A
6

Add

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

to you pom

Akimbo answered 18/3, 2022 at 16:24 Comment(3)
I get same error. But when I use latest version 2.11.0, then I got this error: org.apache.poi.hssf.OldExcelFormatException: The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)Eldwen
I used OldExcelExtractor.Eldwen
fixed the error by specifying a newer version of commons-ioMylohyoid
H
4

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.

Headward answered 11/6, 2022 at 1:18 Comment(0)
F
3

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>
Flan answered 20/2, 2023 at 17:17 Comment(0)
I
2

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

Insurer answered 19/9, 2022 at 8:48 Comment(0)
R
0

It worked for me by removing this dependency

 <dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.4</version>
</dependency>
Rabblement answered 30/11, 2022 at 10:40 Comment(0)
L
0

This issue is resolved with following dependencies: [Dependencies]

Lunular answered 8/4, 2023 at 14:22 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Distend
S
0

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>
Screenplay answered 7/10, 2023 at 2:23 Comment(0)
S
0

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>
Syphilis answered 10/5 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.