Java.lang.NoSuchMethodError: org.apache.poi.xssf.usermodel.XSSFWorkbook.(Ljava/io/InputStream;)V

apache-poiimport-from-exceljava

I have included the dom4j-1-6.jar,poi 3.9 jar,poi ooxml schemas 3.9 jar,poi ooxml 3.9 jar,xmlbeans-2.3.0.jar in my class path.i have used syntax like below

InputStream ExcelFileToRead = new FileInputStream(relative+"/"+docName);
         XSSFWorkbook  wb_hssf  = new XSSFWorkbook(ExcelFileToRead);

Im getting the following error,

java.lang.NoSuchMethodError: org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(Ljava/io/InputStream;)V
    at com.admin.upload_images.doPost(upload_images.java:1057)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)

Best Answer

There's an Apache POI FAQ entry covering virtually this case, which you should read. Basically though, at runtime, you are not using the POI jars you compiled against. Instead, you are somehow using a different version. This could be because you deployed different jars, it could be because your runtime or container or framework shipped other POI jars, or some other mistake. As per this POI FAQ all POI jars must be from the same version too.

So, you need to review your classpath at runtime, with the help of the snippet from the FAQ if you can't spot what's wrong, then remove the mis-matched POI jars so you only have the ones you used at compile time. Your code will then work

However... There are a few other issues with your code! One is that you're using an InputStream when you have a file. This, as explained in the Apache POI docs is bad as it uses lots more memory. Change it to use a File instead, eg with this

Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));

Secondly, Apache POI 3.9 is a bit old now, and there have been a huge number of bug fixes since then, so you should upgrade to the latest version (3.11 or 3.12 beta 1 as of writing)

Related Topic