尝试使用Apache Poi API读取Excel工作表时出现NoClassDefFoundError错误

时间:2018-11-04 15:04:19

标签: java apache-poi xssf

主机:Ubuntu 18.04 openjdk 10.0.2 2018-07-17

我正在尝试使用Apache POI从Excel工作表中读取数据,但出现NoClassDefFoundError。

这是我的代码:

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)
        {

        }



    }

}

这是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

以下是链接到项目的库(使用Eclipse IDE): enter image description here

enter image description here

编辑

将Java更改为1.8后,现在出现此错误:

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)

3 个答案:

答案 0 :(得分:1)

  

由于:java.lang.ClassNotFoundException:   org.apache.commons.compress.utils.InputStreamStatistics

我在引用的库列表中没有找到commons-compress库,因此请尝试添加commons-compress 1.18作为依赖项

答案 1 :(得分:0)

我在POI 4.1.0中遇到了同样的问题,改回3.17解决了该问题。

答案 2 :(得分:0)

我有同样的问题。用3.17替换了POI 4.1,此问题已解决。请添加以下依赖项:-

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