0

Host machine : Ubuntu 18.04 openjdk 10.0.2 2018-07-17

I am trying to read data from an Excel sheet using Apache POI but getting the NoClassDefFoundError.

This is my code :

package readatafromexcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class ReadExcel {

    public static void main(String[] args) throws Throwable {
        // TODO Auto-generated method stub

        try{

        //create file object
        File src = new File("/home/nisha/Downloads/files/First/refdata.xlsx");

        //create file Input stream object
        FileInputStream reader =new FileInputStream(src);

        //create workbook object
        XSSFWorkbook wb  =new XSSFWorkbook(reader);

        //create sheet 
        XSSFSheet sheet1=wb.getSheetAt(0);


        //fetch data from excel
        String rowData =sheet1.getRow(0).getCell(0).getStringCellValue();

        //print value
        System.out.println("data"+rowData);
        }

        catch  (FileNotFoundException e)
        {

        }



    }

}

This is the stacktrace :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/utils/InputStreamStatistics
    at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.<init>(ZipArchiveThresholdInputStream.java:62)
    at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:180)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
    at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:307)
    at readatafromexcel.ReadExcel.main(ReadExcel.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.utils.InputStreamStatistics
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    ... 7 more

Following are the libraries linked to the project (using Eclipse IDE): enter image description here

enter image description here

EDIT

After changing the Java to 1.8 I now get this error :

Exception in thread "main" java.lang.IllegalArgumentException: InputStream of class class org.apache.commons.compress.archivers.zip.ZipArchiveInputStream is not implementing InputStreamStatistics.
    at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.<init>(ZipArchiveThresholdInputStream.java:63)
    at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:180)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
    at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:307)
    at readatafromexcel.ExcelReadData.main(ExcelReadData.java:24)
Naveen
  • 7,944
  • 12
  • 78
  • 165

3 Answers3

3

Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.utils.InputStreamStatistics

I didn't find a commons-compress library in the list of referenced libraries, so try to add a commons-compress 1.18 as a dependency

Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
  • I have added commons-compress 1.18 still I am getting error:`Exception in thread "main" java.lang.IllegalArgumentException: InputStream of class class org.apache.commons.compress.archivers.zip.ZipArchiveInputStream is not implementing InputStreamStatistics – Naveen Nov 04 '18 at 17:10
  • 1
    Well it's another one exception. I guess you are using a Java 10 because fluent googling says that this error occurs on a J10, furthermore I was trying to reproduce the error with Java 8 and didn't succeed. [Here is](https://stackoverflow.com/questions/52467360/flink-inputstream-of-class-class-org-apache-commons-compress-archivers-zip-zipfi) a relevant question, probably it will help – Yuriy Tsarkov Nov 04 '18 at 17:46
  • please see the edit. I am still getting the same error – Naveen Nov 05 '18 at 17:30
  • Sorry but I cann't reproduce the error. Did you really have a commons-compress 1.18_ ? And another one case - you must get rid of an older versions of this library. What if you'll try to downoad a librariy [directly](https://stackoverflow.com/questions/52577367/getting-error-with-apache-poi-which-is-not-able-to-read-file-passed-using-filein) ? [Here is](https://stackoverflow.com/questions/52577367/getting-error-with-apache-poi-which-is-not-able-to-read-file-passed-using-filein) another one topic with a same problem. – Yuriy Tsarkov Nov 06 '18 at 07:57
2

I had the same problem with POI 4.1.0, switching back to 3.17 solved the issue.

0

I had the same issue. Replaced POI 4.1 With 3.17 and the issue got resolved. Please add the below dependency :-

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