【问题标题】:NoClassDefFoundError in Apache POI/FileOutputStreamApache POI/FileOutputStream 中的 NoClassDefFoundError
【发布时间】:2021-07-23 17:26:43
【问题描述】:

当我尝试运行以下代码时,我得到 NoClassDefFoundError 错误。我检查了大量类似的帖子,但没有帮助。我错了什么?我认为,FileOutputStream 有一些东西,但我不知道是什么。

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {


        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Vienna");
        File viennaBatch = new File("C:\\Users\\majan\\Desktop\\CitiJanus\\Vienna.xls");
        viennaBatch.createNewFile();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);
        cell.setCellValue("a");

        
        FileOutputStream fileOut
                = new FileOutputStream("C:\\Users\\majan\\Desktop\\CitiJanus\\Vienna.xls");
        workbook.write(fileOut);
        fileOut.close();
    }
}```

error:

```Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
    at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
    at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
    at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
    at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:99)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:121)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1357)
    at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 8 more```



【问题讨论】:

  • POI 有自己的几个依赖项。我建议你使用 Maven 来处理它们。顺便说一句,您不需要在 Windows 路径上使用反斜杠 - 让自己更轻松并使用 C:/foo/bar

标签: java apache-poi


【解决方案1】:

引用the POI page:

Apache commons-math3 和 commons-compress 添加为依赖项 在 POI 4.0.0 中。 Zaxxer SparseBitSet 作为 POI 中的依赖项添加 4.1.2

您得到的异常表明类路径中缺少 commons-math3。

【讨论】:

  • 非常感谢! :-)
猜你喜欢
  • 1970-01-01
  • 2016-12-22
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多