使用 POI 处理 doc 文件时如何解决 NoSuchMethodError [英] How to solve a NoSuchMethodError when using POI for doc files

查看:67
本文介绍了使用 POI 处理 doc 文件时如何解决 NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试实现以下任何代码时

When I was trying to implement any code of the following

File someFile = new File("D:\\arz.doc");
InputStream inputStrm = new FileInputStream(someFile);
HWPFDocument wordDoc = new HWPFDocument(inputStrm);
System.out.println(wordDoc.getText());

或:

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("D:\\arz.doc"));
WordExtractor extractor = new WordExtractor(fs);
String wordText = extractor.getText();

,错误信息总是如下:

Exception in thread "main" java.lang.NoSuchMethodError:            
org.apache.poi.poifs.filesystem.POIFSFileSystem.getRoot()Lorg/apache/poi/poifs/filesystem/DirectoryNode;
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:186)
at DB_connect.dissertation_araalz.ParseWodDocFile.main(ParseWodDocFile.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

你能帮我解决这个问题吗?

Could you please help me in that problem?

推荐答案

几乎可以肯定,您的类路径上有两个 POI 副本.一种是新的、最新的版本,其中包含您要使用的功能.另一个是旧版本,没有,而且您的系统似乎更喜欢旧版本...

You almost certainly have two copies of POI on your classpath. One is the new, latest version which contains the feature you want to use. The other is an older version that doesn't, and it seems your system is preferring the older one...

这是一个很常见的问题,POI FAQ 涵盖了这种情况.理想情况下,只需查看您的类路径,并尝试识别额外的旧 POI jar.但是,如果这不起作用,请尝试使用 POI 常见问题中的这段代码:

This is a common enough problem that the POI FAQ Covers this very case. Ideally, just look at your classpath, and try to identify the extra older POI jar. However, if that doesn't work, try this snippet of code from the POI FAQ:

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);

这将打印出您正在使用的 POI jar 的文件名,因此您可以找出旧副本的来源并将其删除!

That will print out the filename of the POI jar you're using, so you can work out where the older copy is coming from and remove it!

这篇关于使用 POI 处理 doc 文件时如何解决 NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆