请我在错误上需要一些帮助。我有一个带有以下jar文件的javafx项目
我需要的是将一些Excel数据导入其中。我已经导入了上面的jar文件
但不幸的是,当我尝试运行该项目时,我收到错误:
Caused by: java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Workbook.sheetIterator()Ljava/util/Iterator;
at app.controllers.ContentAreaController.nextActionReport(ContentAreaController.java:734)
... 62 more
我试过谷歌搜索,建议我更改poi lib文件的版本,但没有运气。任何人都建议我解决方案,因为我已经花了足够的时间在这个问题上
答案 0 :(得分:2)
在答案中提出一些评论 - 您的类路径上有较旧的Apache POI jar。根据{{3}}
您需要做的是只是删除旧的POI广告。我说只是,因为你不知道你有他们......幸运的是,this POI FAQ - mixing POI jars between versions is not supported。在有问题的系统上运行时,这样的东西应该打印出旧罐子的名称和位置:
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("POI Core came from " + path);
classloader = org.apache.poi.POIXMLDocument.class.getClassLoader();
res = classloader.getResource("org/apache/poi/POIXMLDocument.class");
path = res.getPath();
System.out.println("POI OOXML came from " + path);
classloader = org.apache.poi.hslf.usermodel.HSLFSlideShow.class.getClassLoader();
res = classloader.getResource("org/apache/poi/hslf/usermodel/HSLFSlideShow.class");
path = res.getPath();
System.out.println("POI Scratchpad came from " + path);
只需识别您不想要,删除的旧罐子,就应该设置好!
答案 1 :(得分:2)
Gagravarr的答案https://stackoverflow.com/a/50472754/1497139指向http://poi.apache.org/help/faq.html#faq-N10006
我修改了用于JUnit测试的代码,以解决以下错误消息,从而解决同一基本问题:
java.lang.NoSuchMethodError: org.apache.poi.xssf.usermodel.XSSFCell.getCellTypeEnum()Lorg/apache/poi/ss/usermodel/CellType;
首先想到的是弃用问题。解决折旧问题后,我得到了:
java.lang.NoSuchMethodError: org.apache.poi.xssf.usermodel.XSSFCell.getCellType()Lorg/apache/poi/ss/usermodel/CellType;
这是由于在同一项目中混合了Apache POI 3.12和Apache POI 4.0.1 jars。
为了将来避免这种情况,我创建了以下单元测试。您可能想要使版本适应您的需求,或者在仍然调试问题的同时完全跳过断言。
JUnit测试以检查所用类的POI版本。
/**
* get the path the given class was loaded from
*
* @param clazz
* @return the path
*/
public String getClassLoaderPath(Class<?> clazz) {
ClassLoader classloader = clazz.getClassLoader();
String resource = clazz.getName().replaceAll("\\.", "/") + ".class";
URL res = classloader.getResource(resource);
String path = res.getPath();
return path;
}
@Test
public void testPOI() {
Class<?>[] classes = {
org.apache.poi.poifs.filesystem.POIFSFileSystem.class,
org.apache.poi.ooxml.POIXMLDocument.class,
org.apache.poi.hslf.usermodel.HSLFSlideShow.class,
org.apache.poi.xssf.usermodel.XSSFCell.class,
org.apache.poi.ss.usermodel.CellType.class};
for (Class<?> clazz : classes) {
String path = getClassLoaderPath(clazz);
if (debug)
System.out.println(
String.format("%s came from %s", clazz.getSimpleName(), path));
assertTrue(path.contains("4.0.1"));
}
}