Thursday 20 September 2018

How to resolve Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile while reading data from Apache POI

How to resolve Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile while reading data from Apache POI


Step1: Download Apache POI libraries from https://poi.apache.org/ For eg:
https://poi.apache.org/download.html#POI-4.0.0

.tar.gz or .zip depending on the Linux or Windows OS you are working respectively.


Step2: Extract downloaded files and import them in your eclipse project.
Step3: While executing sample code,

String projectDir=System.getProperty("user.dir");
File file = new File(projectDir+"/Resources/TestData.xlsx");

InputStream is = new FileInputStream(file);

XSSFWorkbook workbook = new XSSFWorkbook(is);
XSSFSheet sheet = workbook.getSheet("Sheet1");


// index of rownos
int lastRowNum = sheet.getLastRowNum();

System.out.println(lastRowNum);

for(int i=0;i<=lastRowNum;i++)
{
XSSFRow row = sheet.getRow(i);

// cell count
short lastCellNum = row.getLastCellNum();

for(int j=0;j<lastCellNum;j++)
{
String excelData = row.getCell(j).toString();
System.out.println(excelData);

// XSSFCell cell = row.getCell(j);

//cell.setCellValue("data");

}
}


Error appears:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile
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 datasource.Reading_XLS.main(Reading_XLS.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipFile
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more


Step4: To resolve above problem, download Apache common compress jars from

https://commons.apache.org/proper/commons-compress/download_compress.cgi


Step5: Download and import "commons-compress-1.18.jar" in the eclipse project along with previously imported jars. 


Error no longer appears and data will be read / written from excel. 


Hope this blog has been useful to you.

Youtube channel: https://www.youtube.com/user/srinivaskinik
Facebook page: https://www.facebook.com/srinivaskinikalmady/



No comments:

Post a Comment

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...