0

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!

  • 1
    Possible duplicate of [How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9](https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j) – Borislav Stefanov May 13 '19 at 07:38
  • 2
    Your code seems correct. `NoClassDefFoundError` means that you haven't imported correctly `Apache POI` binary in classpath. – leopal May 13 '19 at 07:40
  • 1
    You are using an `XSSFWorkbook` but you are storing it in `.xls` format. Provide another `x` and make it `.xlsx`. Have a look at [some information about this topic given by apache](https://poi.apache.org/components/spreadsheet/). This is a different issue and not directly related to this question. – deHaar May 13 '19 at 07:44
  • 1
    "I get a NoClassDefFoundError": Please show the stacktrace. At least we need to know what class cannot be found. – Axel Richter May 13 '19 at 08:13
  • 1
    Not a missing poi dependency... [commons-collections maven](https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.0). Though you don't include that one in your code. You either are running another class or you still have it imported – XtremeBaumer May 13 '19 at 08:25

2 Answers2

2

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();
 } 
}
Jagadeesh
  • 358
  • 5
  • 17
  • Apache POI 3.9 is ancient! Why on earth are you suggesting someone use it, rather than the latest stable one? – Gagravarr May 14 '19 at 13:36
2

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>
Jochen Reinhardt
  • 833
  • 5
  • 14