javafx - 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,我正在使用以下库:
- xmlbeans-3.1.0.jar
- poi-4.1.2.jar
- poi-excelant-4.1.2.jar
- poi-ooxml-schemas-4.1.2.jar
- commons-collections4-4.4.jar
- commons-compress-1.19.jar
- commons-logging-1.2.jar
- poi-ooxml-4.1.2.jar
和 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>
解决方案
推荐阅读
- java - Android CalendarView 年份文本颜色
- java - 机器人硬币收集问题动态编程
- python-3.x - Python - 如何以给定数字作为 n 的值来计算 n+nn+nnn+nnnn 的值
- java - 如何测试 Quarkus REST 客户端接口?
- python - 如何在 pandas DataFrame 中创建具有条目计数的列?
- databricks - DBFS:如何在 Datarbricks 中获取 DBFS 文件的上次访问时间
- swiftui - 在 SwiftUI Text 中获取单词的框架
- c# - 简单的插件架构实现——传递实例
- oauth-2.0 - 将用户池“应用程序客户端 ID”设置为 HTTP API AWS YAML 的受众 - 令牌没有有效的受众
- php - 跳转到另一个页面时php cookie更改为NULL