java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook

自闭症网瘾萝莉.ら 提交于 2020-01-14 07:41:10

问题


when i tried to create a workbook in normal java class it is creating but when i tried to run the same on servlet it is throwing exceptions

Normal java class

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Sampleclass {

    public static void main(String[] args) {

        FileInputStream file;
        try {
            file = new FileInputStream(new File("C:\\Users\\Rajasekhar\\Desktop\\EmployeeDetails.xlsx"));
            Workbook book=new XSSFWorkbook(file);
            System.out.println(book);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Servlet

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


@SuppressWarnings("serial")
public class sampleServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
             {
        FileInputStream file;
        try {
            file = new FileInputStream(new File("C:\\Users\\Rajasekhar\\Desktop\\EmployeeDetails.xlsx"));
            Workbook book=new XSSFWorkbook(file);

        } catch (FileNotFoundException e) {
            System.out.println("exception occured is "+e);      
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("exception occured is "+e);      
            e.printStackTrace();
        }
    }

}

the exception occured was

SEVERE: Servlet.service() for servlet [upload] in context with path [/Temp2] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    at sampleServlet.doGet(sampleServlet.java:23)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

I have added all required apache poi jars.


回答1:


Download poi-ooxml.jar and add it to your classspath.




回答2:


You need the file poi-ooxml.jar in your classpath as @Jens said.

You are using the correct import statement but it seems that you don't have the file poi-ooxml.jar in the class path of your web container.

If you are using maven, you should put this dependency in your pom.xml inside the <dependencies> tag:

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

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



回答3:


i know this may be late but the problem was that you may ignore the file xmlbeans-2.6.0.jar which is in separate file from other apache jar (ooxml-lib)



来源:https://stackoverflow.com/questions/26992908/java-lang-classnotfoundexception-org-apache-poi-xssf-usermodel-xssfworkbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!