menu

Questions & Answers

java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook

I'm trying to access an excel file the Apache Workbook module. The code runs perfectly sometimes, but mostly I get an error saying:

exception

javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook

root cause

java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook

I looked it up and read that it is a jar file problem. Tried to fix it from my side but I'm still getting the same error.

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>XYZ</groupId>
    <artifactId>ABC</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>ScriptsGenerator Maven Webapp</name>
    <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
      <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.15</version>
      </dependency>
    <dependency>
        <groupId>org.lucee</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.15.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
            <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.8</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
    <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-collections4</artifactId>
       <version>4.1</version>
    </dependency>
    <dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.2</version>
  </dependency>

    </dependencies>
    <build>
      <finalName>AAAA</finalName>
    </build>
  </project>

Where am I going wrong?

Answers(4) :

Please check whether the apache poi jars got downloaded,

Also try using the below dependecy for poi-ooxml-schemas

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.15</version>
</dependency>
Comments:
2023-01-18 00:10:06
I went to Properties > Java Build Path > Libraries. I see the jars under Maven Dependencies.

It looks like you're running this in a servlet container, which probably means that container has its own set of jar files. With Tomcat, for instance, that would be in the Tomcat/lib directory. Check which jars your servlet container is using, to see if one of them is incompatible with what you're using.

What you'd be looking for is an old version of poi that doesn't have the class you need. If such a jar exists and gets loaded before the correct one, it can cause the problems you're seeing, even though your web app itself has the right version in it.

Comments:
2023-01-18 00:10:06
I'm not sure how to check if the jars are compatible. Can you please elaborate?

Clean your project, delete content of {User_Directory}/.m2 folder and re-run your project

Under windows system you can find maven repo path as C:\Users\{user_name}\.m2

Under linux system you can find maven repo path as /home/{user_name}/.m2

Comments:
2023-01-18 00:10:06
I don't see the .m2 folder. Can you help me with it?
2023-01-18 00:10:06
If you didn't changed the maven settings forcefully, the .m2 folder must be in your user/home directory.

Clean your project, delete content of {User_Directory}/.m2 folder and re-run your project , This method works for me .