0

I'm trying to create a file in Java. I have downloaded the lastest Apache POI version and kinda having troubles with all the "build path" thing. I'm not sure if I was doing everything right and not sure what jar files i should use. I try to run the code and thats the error I get:

Error: Unable to initialize main class TestCaused by: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook

My code:

import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {
public static void main(String[] args) throws IOException {
    Workbook workbook = new XSSFWorkbook("Test.xlsx");
    Sheet sheet = workbook.createSheet("SheetTest");
    Row headerRow = sheet.createRow(0);
    for (int i = 0; i < 5; i++) {
        Cell cell = headerRow.createCell(i);
        cell.setCellValue(i);
    }
    workbook.close();
}
}

Maybe I have a problem with the classpath? How can I change it? If that's not the problem, Does anyone have an idea?

Hagai
  • 11
  • 4

2 Answers2

0

firstly be sure that you are using the latest version of this library in case you are using maven these dependencies helping you

 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version>
    </dependency>

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

also you have to create a file with .xls or .xlsx extensions you can use something like this

Workbook workbook = WorkbookFactory.create(new File(fileName));

Sheet sheet = workbook.createSheet("sheetName");
int rowIndex = 0;
// this is an example from the official site  iterate over the list  and fill the  sheet 
                while(iterator.hasNext()){
                    Country country = iterator.next();
                    Row row = sheet.createRow(rowIndex++);
                    Cell cell0 = row.createCell(0);
                    cell0.setCellValue(country.getName());
                    Cell cell1 = row.createCell(1);
                    cell1.setCellValue(country.getShortCode());
                }
//lets write the excel data to file now
        FileOutputStream fos = new FileOutputStream(fileName);
        workbook.write(fos);
        fos.close();
        System.out.println(fileName + " written successfully");
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Those versions are ancient! They are [over 5 years old, with loads of bugs found and fixed](http://poi.apache.org/changes.html#3.10-FINAL)! Why on earth are you suggesting people use old versions? – Gagravarr Feb 25 '19 at 17:09
  • Also, why are you doing the format detection by hand? Why not use `WorkbookFactory` instead? – Gagravarr Feb 25 '19 at 17:09
  • See [the Apache POI FAQ](http://poi.apache.org/help/faq.html) - the `poi` and `poi-ooxml` jar versions need to match. You also need to update to use `WorkbookFactory` to detect the file type – Gagravarr Feb 25 '19 at 22:26
  • @Gagravarr those jar files are the same version, what can i do? – Hagai Feb 25 '19 at 23:11
  • @HazemMusallam what is this maven for? I don't know what is it. how can i use the apache poi I have downloaded? I have the last version of this API – Hagai Feb 25 '19 at 23:13
  • Maven is like un organizer to your jars read about maven is worthy – Hazem Musallam Feb 26 '19 at 13:03
0

Probably, you forgot to add some jar files in the classpath. Try to follow these steps:

  1. Right-click on your Project name in Package Explorer tab -> Build Path -> Configure Build Path

Step 1

  1. In "Libraries" tab, remove your old POI library
  2. Click on "Add Library" -> User Library -> User Libraries

Step 3

  1. Remove your old libraries and click on "New". Then insert the name of the library and press "ok".
  2. Select your new library and click on "Add External Jars".

Another step

  1. Browse to your extracted POI path and select all the jar files, also in the subfolders!
  2. Now click on Apply and close and select the new library on the previous window.

Now it should work!

Calaf
  • 1,133
  • 2
  • 9
  • 22