0

I have downloaded the latest release of poi-5.0.0 and add the external jar to the Java Project. Here is the code:

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
public class Test {

    public static void main(String[] args) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("FirstSheet");
            
            FileOutputStream fileOut = new FileOutputStream("NewExcelFile1.xlsx");
            workbook.write(fileOut);
            fileOut.close();
            System.out.println("Success");
        } catch (IOException e) {
            e.printStackTrace();
            }
        }
}  

The Error I am getting is:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook at test3.Test.main(Pleease.java:11) Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 1 more

Could not find the solution for this issue on the internet so I would be very happy to find it here. Thanks in advance!

LLnnRR
  • 15
  • 3
  • 1
    You need to add more libraries, not only poi-5.0.0. Check the last part of [this answer](https://stackoverflow.com/questions/65940340/how-to-update-an-excel-file-in-java/65945983#65945983) ("Some final notes") for the list (with all links) of all libraries that i usually use when working with excel from java using ApachePoi. Also, i suggest you to use poi-4 (not poi-5) and [XSSF objects (not HSSF)](https://stackoverflow.com/questions/45936257/apache-poi-hssf-vs-poi-xssf) expecially if you are using .xlsx files! – VinceLomba Feb 25 '21 at 18:55
  • I tried everything you suggested, but it still gives the same error (this time the HSSF changed to XSSF: "java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook") – LLnnRR Feb 25 '21 at 19:15
  • 1
    `NoClassDefFoundError` occurs if libraries which were present at compile time are not present at run time. So your `Eclipse` has access to `apache poi` libraries at compile time only. – Axel Richter Feb 26 '21 at 05:57
  • Did you add all the EIGHT libraries that are listed [in the post i linked](https://stackoverflow.com/questions/65940340/how-to-update-an-excel-file-in-java/65945983#65945983) AND also did you switch to Apache Poi 4 (and not 5)? "java.lang.NoClassDefFoundError" means that you're missing some libraries (it is happened to me a couple of times). Also, did you [add properly all the 8 required libraries to your eclipse project](https://stackoverflow.com/questions/2824515/how-to-add-external-library-properly-in-eclipse)? – VinceLomba Feb 26 '21 at 07:50
  • Yes, I added 8 JAR files (which are linked in the post) to the new folder "lib" and then added them as External JARS to the eclipse. I did download apache poi 4.1.2, but it still shows the same error. At this point it makes me assume that there is no solution to this – LLnnRR Feb 26 '21 at 09:57
  • 1
    There is always a solution! If you're still getting the *same* error it probably means that you're importing the libraries in your eclipse project not in the right way, maybe you can edit your question adding some images about your project importing settings in order to check if they are okay (maybe you add the to the modulepath and not to the classpath?)! – VinceLomba Feb 27 '21 at 09:55

1 Answers1

0

Reproduced this. When you are adding apache .jar file dependencies after right clicking on the Project -> Properties -> Java Build Path, make sure you are adding the .jars to the ClassPath and not Modulepath. If you add them to the Modulepath, this error shows up. Also if you have a module dependencies class, recreate your eclipse project and deselect the "Generate module" option checkbox. This is a work around until modules are sorted out.