John Wambura
John Wambura

Reputation: 47

How do I fix the NoClassDefFoundError in java Apache POI project?

I have been battling to fix the NoClassDefFoundError exception without success. Getting a bit frustrated now. I am creating an application to read excel files using Apache POI library. This is what I am doing:

  1. Downloaded the binary distribution of the library and saved it on a desktop folder called Apache POI library

  2. Extracted files from the zip folder

  3. I created a new project on Netbeans and called it apachePOI

  4. On the project navigation tab of Netbeans for my project, I went to right click libraries>Add jar/Folder.. and imported all .jar files from the downloaded library. Added a total of 11 .jar files

  5. I went to USER environment variables, and chose to add a new environment variable called classpath. On the variable value, I added the path to each of the every 11.jar files in my library. One such path is C:\Users\wks\Desktop\Apache POI Library\poi-bin-3.17-20170915\poi-3.17. I separated the paths with a semicolon

  6. Thereafter, I created a very simple TestClass class which is compiling fine but failing to run with an error NoClassDefFoundError.The code in my class does nothing other than declaring an XSSFWorkbook like XSSFWorkbook workbook = new XSSFWorkbook(file).

I am not using maven and had chosen not to use. Cannot figure out what I am doing wrong, though I suspect I am getting it wrong adding my library to the classpath as in step (5) above

Upvotes: 2

Views: 20469

Answers (2)

Eswara Srisai
Eswara Srisai

Reputation: 1

Download poi-ooxml JAR 4.1.2 with all dependencies, which doesn't throw any ClassNotFoundException https://jar-download.com/artifacts/org.apache.poi/poi-ooxml/4.1.2/source-code

Upvotes: 0

Michael Dz
Michael Dz

Reputation: 3834

Most of the time this error happens when some dependencies are missing. I can see that you are trying to use XSSFWorkbook but according to Apache's website this component requires additional dependency poi-ooxml. You should also add xmlbeans-2.3.0.jar:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.3.0</version>
</dependency>

You can find a list of all required dependencies under this link.

In my opinion you should start using Maven even for a simple program. It's a very useful tool and necessary for big projects.

Upvotes: 6

Related Questions