Apache POI - 给出 NoclassDefError

Apache POI - Giving NoclassDefError


    public static void main(String [] args){
        String excelFilePath = "D:\JavaBooks.xls";

        try {
            FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
            Workbook workbook = WorkbookFactory.create(inputStream);

            Sheet sheet = workbook.getSheetAt(0);

            Object[][] bookData = {
                    {"The Passionate Programmer", "Chad Fowler", 16},
                    {"Software Craftmanship", "Pete McBreen", 26},
                    {"The Art of Agile Development", "James Shore", 32},
                    {"Continuous Delivery", "Jez Humble", 41},
            };

            int rowCount = sheet.getLastRowNum();

            for (Object[] aBook : bookData) {
                Row row = sheet.createRow(++rowCount);

                int columnCount = 0;

                Cell cell = row.createCell(columnCount);
                cell.setCellValue(rowCount);

                for (Object field : aBook) {
                    cell = row.createCell(++columnCount);
                    if (field instanceof String) {
                        cell.setCellValue((String) field);
                    } else if (field instanceof Integer) {
                        cell.setCellValue((Integer) field);
                    }
                }

            }

            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream("D:\JavaBooks.xls");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();

        } catch (IOException | EncryptedDocumentException
                ex) {
            ex.printStackTrace();
        }
    }

}

While executing this code it is giving 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.(DirectoryProperty.java:52) at org.apache.poi.poifs.property.RootProperty.(RootProperty.java:31) at org.apache.poi.poifs.property.PropertyTable.(PropertyTable.java:58) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:102) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:274) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:221) at NewClass.main(NewClass.java:31) Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 9 more


将 Apache 通用数学添加到您的依赖项中。

例如在 Maven 中:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-math3</artifactId>
  <version>3.6.1</version>
</dependency>

此处有更多选项: https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1

可以在此处找到有关 ClassNotFoundErrors 的更多信息:Why am I getting a NoClassDefFoundError in Java?