Yash Yelmame
Yash Yelmame

Reputation: 89

How To fix NoClassDefFoundError while creating an Excel Spreadsheet using Apache POI in java?

I just started learning Apache POI I want to make a super simple spread sheet something like this in excel,

+----------+----------+----------+----------+----------+
|    1     |    2     |    3     |    4     |    5     |
+----------+----------+----------+----------+----------+

the code I wrote is as follows,

public void exportTable() throws IOException
{
    FileOutputStream fos=new FileOutputStream(new File("C:\\Users\\*****\\OneDrive\\Desktop\\excel.xlsx"));
    XSSFWorkbook wb=new XSSFWorkbook();
    XSSFSheet ws=wb.createSheet();
    XSSFRow row=ws.createRow(0);
    for(int i=0;i<=5;i++)
    {
        Cell cell=row.createCell(i);
        cell.setCellValue(i);
    }
    wb.write(fos);
    fos.close();
}

I get a NoClassDefFoundError

I am using the following jar files.

1)poi-4.1.0.jar

2)poi-examples-4.1.0.jar

3)poi-excelant-4.1.0.jar

4)poi-ooxml-4.1.0.jar

5)poi-ooxml-schemas-4.1.0.jar

6)poi-scratchpad-4.1.0.jar

7)xmlbeans-3.1.0.jar

8)curvesapi-1.06.jar

And I am using jdk 1.8 on netbeans

P.S. I have near zero experience with Apache POI so I would be gratefull if you would write a detailed answer.Thanks

P.P.S. I don't know Maven

Edit: So i changed a few things

1)I imported all the jar files from "common-collections".

2)I changed ".xls" to ".xlsx"

3)I also Imported "common-compress" and now it works perfectly fine. Thanks!

Upvotes: 0

Views: 634

Answers (2)

Jagadeesh
Jagadeesh

Reputation: 398

That error will be thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

just change your destination file extension to .xlsx and it should work.

i suggest you instead of adding jar files, just convert to maven project. maven project is pretty awesome to hold the project & dependency management.

and add the below dependency for apache POI

<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-collections4</artifactId>
 <version>4.0</version>
</dependency>

Now you can work with XSSFWorkbook easily. Just see the below code for your reference which is working fine from my end.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestXSSF {
	static XSSFWorkbook workbook;
	static FileInputStream file;

	public static void main(String args[]) throws IOException {
		FileOutputStream fos=new FileOutputStream(new File(System.getProperty("user.dir")+"/input_data/excel.xlsx"));

		XSSFWorkbook wb=new XSSFWorkbook();
		XSSFSheet ws=wb.createSheet();
		XSSFRow row=ws.createRow(0);
		for(int i=0;i<=5;i++)
		{
			Cell cell=row.createCell(i);
			cell.setCellValue(i);
		}
		wb.write(fos);
		fos.close();
	}	
}

Upvotes: 2

Jochen Reinhardt
Jochen Reinhardt

Reputation: 843

The error message states: java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap

That's from Apache's commons-collections4 library. https://commons.apache.org/proper/commons-collections/ It seems you do not have this lib on your class path.

For maven, use this dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.3</version>
</dependency>

Upvotes: 2

Related Questions