如何避免 Apache POI 中的 java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18231134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;) in Apache POI
提问by Avinash
I have a code for adding watermark to existing .doc file.
我有一个将水印添加到现有 .doc 文件的代码。
The following is the code I have tried so far
以下是我迄今为止尝试过的代码
public static void main(String[] args)
{
try
{
XWPFDocument xDoc = new XWPFDocument(new FileInputStream("test.doc"));
XWPFHeaderFooterPolicy xFooter = new XWPFHeaderFooterPolicy(xDoc);
xFooter.createWatermark("My Watermark");
}
catch(Exception e) {
e.printStackTrace();
}
}
The following is what I got
以下是我得到的
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)V
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:50)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:71)
at com.avi.Test.ReadDoc.main(Watermark.java:38)
回答by M. Abbas
You almost certainly have an older version of POI on your classpath.
几乎可以肯定,您的类路径上有旧版本的 POI。
回答by Gagravarr
See the Apache POI FAQ entry on this very topic. What has almost certainly happened is that you have added a new copy of POI to your classpath, but an older version was already there (from an earlier need, your framework etc), and Java is now getting confused about which one to use.
请参阅有关此主题的Apache POI FAQ 条目。几乎可以肯定发生的是,您已经在类路径中添加了一个新的 POI 副本,但旧版本已经存在(来自早期的需求、您的框架等),而 Java 现在对使用哪个版本感到困惑。
Firstly, you'll want to use a snippet of code like this to work out where POI is coming from:
首先,您需要使用这样的一段代码来确定 POI 的来源:
ClassLoader classloader =
org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
"org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("Core POI came from " + path);
Use that to identify the older jar(s) and remove them.
使用它来识别较旧的罐子并将其移除。
Then, use the POI Components Pageto work out what Jars you need to use, and what their dependencies are. Finally, add the latest jars to your classpath, and you'll be good to go!
然后,使用POI 组件页面来确定您需要使用哪些 Jars,以及它们的依赖项是什么。最后,将最新的 jar 添加到您的类路径中,您就可以开始使用了!
回答by venkyreddy
I got same problems like you,the solution is you need to import all jar files to run your program.These are mandatory for running your project
我遇到了和你一样的问题,解决方案是你需要导入所有的 jar 文件来运行你的程序。这些是运行你的项目所必需的
- Poi-3.10-Final.jar
- Poi-ooxml-3.10-Final.jar
- Poi-ooxml-schemas-3.10.jar
- Xmlbeans-2.30.jar
- Poi-3.10-Final.jar
- Poi-ooxml-3.10-Final.jar
- Poi-ooxml-schemas-3.10.jar
- xmlbeans-2.30.jar
回答by Xelian
Go here: http://poi.apache.org/download.html
去这里:http: //poi.apache.org/download.html
download the tar.gz -> extract it and add to the build classPath all the jars from it.
下载 tar.gz -> 将其解压缩并将其中的所有 jar 添加到构建类路径中。
回答by sofia
I got this error today: "java.lang.NoSuchMethodError:org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V]"
我今天收到这个错误:“java.lang.NoSuchMethodError:org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V]”
It looks different from your error, but quite similar. FYI, I'm using maven to manage jars. After some experiment, I found out the root case is the poi.jar and poi-ooxml.jar's version are not consistent.
它看起来与您的错误不同,但非常相似。仅供参考,我正在使用 maven 来管理罐子。经过一些实验,我发现根本情况是poi.jar和poi-ooxml.jar的版本不一致。
This configuration will get an error:
这个配置会报错:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
I changed the version of poi.jar from 3.12 to 3.13
我将 poi.jar 的版本从 3.12 更改为 3.13
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
bingo, problem solved. I hope this will help someone who ran into this kind of Exception.
宾果游戏,问题解决。我希望这会帮助遇到这种异常的人。
回答by Tiina
From the very beginning, poi-ooxml and poi version must be identical.
从一开始,poi-ooxml 和 poi 版本就必须相同。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version> <---------------this.
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version> <---------------this.
</dependency>
回答by Ziv.Ti
align the pom versions of 'poi' and 'poi-ooxml' it will do the work
对齐 'poi' 和 'poi-ooxml' 的 pom 版本,它将完成工作
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
回答by user26316
I had a similar issue with apache Tika (java.lang.NoSuchMethodError Error): javax.servlet.ServletException: java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.closeQuietly(Ljava/io/Closeable;)
我有一个与 apache Tika 类似的问题 (java.lang.NoSuchMethodError Error): javax.servlet.ServletException: java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.closeQuietly(Ljava/io/Closeable;)
I resolved the issue by updating the POI jars.
我通过更新 POI 罐子解决了这个问题。