首页 > 解决方案 > Java FX 与 Apache POI ClassNotFoundException 冲突

问题描述

我准备了一些读取 Excel/编辑 Word 文件的代码,并希望将其应用于 JavaFX 中的 GUI。“干”代码完美运行,但是当我尝试通过将其附加到 JavaFX 按钮来运行该方法时,出现错误:

Caused by: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
    at application.Controller.btnGenerate(Controller.java:111)
    ... 62 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook

我的 IDE 是 Eclipse,我正在使用以下库:

和 javafx sdk - 11.0.2。如何解决?

@Edit - 添加示例代码:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml"));
        Parent root;
        
            root = (Parent) loader.load();
            primaryStage.setScene(new Scene(root, 300, 300));
            primaryStage.getScene().getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            Controller controller = (Controller) loader.getController();
            controller.setStage(primaryStage);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}
public class Controller {
    @FXML
    private Button btn;
    
    private Stage primaryStage;
    
    public void setStage(Stage stage) {
        this.primaryStage=stage;
    }
    
    @FXML
    public void btnGenerate(ActionEvent e) {
        System.out.println("Btn clicked!");
            try {
                ReadExcel readExcel = new ReadExcel();
                readExcel.readExcel();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
public class ReadExcel {

    public void readExcel() throws FileNotFoundException, IOException {
        
        String path = "path/to/excel";
        
        try (FileInputStream file = new FileInputStream(new File(path)); Workbook workbook = new XSSFWorkbook(file)) {
            DataFormatter dataFormatter = new DataFormatter();
            XSSFSheet worksheet = (XSSFSheet) workbook.getSheetAt(0);
            int lastColumn = worksheet.getRow(0).getLastCellNum();
            for (int colIndex = 0; colIndex < lastColumn; colIndex++) {
                for (int rowIndex = 0; rowIndex < worksheet.getLastRowNum() + 1; rowIndex++) {
                    Row row = CellUtil.getRow(rowIndex, worksheet);
                    Cell cell = CellUtil.getCell(row, colIndex);
                    String cellValue = dataFormatter.formatCellValue(cell);
                    System.out.println(row + " " + cellValue);
                    }
                }
            }
         catch (Exception e) {
            e.printStackTrace();
        }
    }
}

和 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<AnchorPane prefHeight="231.0" prefWidth="238.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <children>
      <Pane layoutX="-11.0" layoutY="-13.0" prefHeight="205.0" prefWidth="220.0">
         <children>
            <Button fx:id="btn" layoutX="84.0" layoutY="78.0" mnemonicParsing="false" onAction="#btnGenerate" prefHeight="41.0" prefWidth="68.0" text="Button" />
         </children>
      </Pane>
   </children>
</AnchorPane>

标签: javafxapache-poiclassnotfoundexception

解决方案


推荐阅读