简体   繁体   中英

JavaFX: Location is not set error in cmd

My project is running perfectly in eclipse but when I run the jar of the project in cmd, I got the error java.lang.IllegalStateException: Location is not set. It seems that my FXML is not loaded correctly. I know this question has been asked many times but none of them can solve my problem. I have tried to use

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

but I still have the error.

Here is the structure of my project

The method Run in 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();
}

What's the wrong of my code?

Simple change

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

to

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

And be careful with the case sensitivity, because you have a capital I in MainWIndowView.fxml but the file is named MainWindowView.fxml .

Thanks to @James_D for pointing out:

When you run during development in Eclipse, it is loading the classes from the file system. So if you use an operating system that treats files the same, irrespective of the case of the file name, then it will find the right resource. Obviously, the same is not true in a jar file (or in any sensible operating system).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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