JavaFX:cmd中的位置未设置错误

时间:2017-12-12 09:08:44

标签: java eclipse javafx javafx-8

我的项目在eclipse中完美运行但是当我在cmd中运行项目的jar时,我收到错误java.lang.IllegalStateException: Location is not set.似乎我的FXML未正确加载。我知道这个问题已被多次询问,但没有一个能解决我的问题。我试过用

FXMLLoader loader = new FXMLLoader(Main.class.getResource("/application/MainWIndowView.fxml"));
AnchorPane root = (AnchorPane) loader.load();

但我仍然有错误。

Here is the structure of my project

方法在eclipse中运行:

public void showMainWIndow() throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(Main.class.getResource("/application/MainWIndowView.fxml"));
    AnchorPane root = (AnchorPane) loader.load();

    MainWindowController controller = loader.getController();
    controller.setMain(this, primaryStage);
    controller.setKeyPress();
    controller.bindWidthAndHeight();

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}

我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

简单的改变

Main.class.getResource("/application/MainWIndowView.fxml")

Main.class.getResource("MainWindowView.fxml")

请注意区分大小写,因为I中有一个大写MainWIndowView.fxml,但文件名为MainWindowView.fxml

感谢@James_D指出:

  

在Eclipse中开发期间运行时,它正在加载类   来自文件系统。因此,如果您使用的是操作系统   文件名相同,无论文件名是什么,那么它   会找到合适的资源。显然,罐子里也是如此   文件(或任何合理的操作系统)。