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

时间:2021-05-01 20:59:49

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

我已经包括了dom4j-1-6。jar,poi 3.9 jar,poi ooxml schema 3.9 jar,poi ooxml 3.9 jar,xmlbeans-2.3.0。jar在我的类路径中。我使用了如下所示的语法

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)

2 个解决方案

#1


1  

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.

有一个Apache POI FAQ条目几乎涵盖了这个案例,您应该阅读它。基本上,在运行时,您没有使用编译过的POI jar。相反,您使用的是不同的版本。这可能是因为您部署了不同的jar,可能是因为您的运行时或容器或框架发送了其他POI jar,或者其他错误。根据这个POI FAQ,所有POI jar都必须来自同一个版本。

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

因此,您需要在运行时检查您的类路径,在FAQ片段的帮助下,如果您不能发现哪里出了问题,那么删除不匹配的POI jar,以便您只有在编译时使用的POI jar。然后您的代码就可以工作了

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

然而……您的代码还有其他一些问题!一个是当你有一个文件时,你正在使用InputStream。正如Apache POI文档中所解释的那样,这很糟糕,因为它使用了大量内存。把它改为使用一个文件(如用这个)

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)

其次,Apache POI 3.9现在有点老了,从那以后有大量的bug修复,所以您应该升级到最新的版本(编写时是3.11或3.12 beta 1)

#2


0  

As the exception suggest even though you are include a version in which the method exists, the build is considering an older version in which the method does not exists. To test this check your maven dependency tree and remove the older versions. It could also be possible that an older version is comming from the container as well so check it properly.

正如异常所提示的,即使您包含了一个方法存在的版本,构建也考虑了一个方法不存在的旧版本。要测试这个,请检查maven依赖树并删除旧版本。也有可能旧版本也来自容器,因此要正确检查它。

#1


1  

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.

有一个Apache POI FAQ条目几乎涵盖了这个案例,您应该阅读它。基本上,在运行时,您没有使用编译过的POI jar。相反,您使用的是不同的版本。这可能是因为您部署了不同的jar,可能是因为您的运行时或容器或框架发送了其他POI jar,或者其他错误。根据这个POI FAQ,所有POI jar都必须来自同一个版本。

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

因此,您需要在运行时检查您的类路径,在FAQ片段的帮助下,如果您不能发现哪里出了问题,那么删除不匹配的POI jar,以便您只有在编译时使用的POI jar。然后您的代码就可以工作了

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

然而……您的代码还有其他一些问题!一个是当你有一个文件时,你正在使用InputStream。正如Apache POI文档中所解释的那样,这很糟糕,因为它使用了大量内存。把它改为使用一个文件(如用这个)

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)

其次,Apache POI 3.9现在有点老了,从那以后有大量的bug修复,所以您应该升级到最新的版本(编写时是3.11或3.12 beta 1)

#2


0  

As the exception suggest even though you are include a version in which the method exists, the build is considering an older version in which the method does not exists. To test this check your maven dependency tree and remove the older versions. It could also be possible that an older version is comming from the container as well so check it properly.

正如异常所提示的,即使您包含了一个方法存在的版本,构建也考虑了一个方法不存在的旧版本。要测试这个,请检查maven依赖树并删除旧版本。也有可能旧版本也来自容器,因此要正确检查它。