读取excel文件时的Apache POI NoClassDefFoundError

时间:2017-03-09 14:51:41

标签: java excel apache apache-poi noclassdeffounderror

我正在尝试运行此代码:

public class ExcelSimpleTest {
    public static void main(String[] args) throws  Exception {
          File f = new File("C:\\Users\\yaron\\Desktop\\Test.xlsx");
          FileInputStream fis = new FileInputStream(f);

          XSSFWorkbook wb=new XSSFWorkbook(fis);
          Sheet sheet=wb.getSheetAt(0);
          System.out.println(sheet.getRow(0).getCell(0).getStringCellValue());
    }

}

我收到此错误:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    at src.ExcelSimpleTest.main(ExcelSimpleTest.java:22)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

我在互联网上查看并复制了Apache POI文件,但不明白我的问题是什么。

1 个答案:

答案 0 :(得分:6)

看起来你错过了类路径中的commons-collection4.jar。下载并将其添加到类路径中。如果你使用maven,你可以添加

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

到你的pom。

如果没有,您可以下载here