繁体   English   中英

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

[英]JavaFX: Location is not set error in cmd

我的项目在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();

但我仍然有错误。

这是我项目的结构

该方法在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();
}

我的代码有什么问题?

简单的改变

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

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

并注意大小写,因为MainWIndowView.fxml有一个大写的I ,但文件名为MainWindowView.fxml

感谢@James_D指出:

当您在Eclipse中进行开发时运行时,它将从文件系统中加载类。 因此,如果使用的操作系统对待文件的方式相同,无论文件名大小写如何,则它将找到正确的资源。 显然,在jar文件(或任何明智的操作系统)中并非如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM