JavaFX executable JAR runs into problems with loading FXML file, why isn't the FXML being loaded?

2.3k views Asked by At

Here is the exception output below. I think the program is failing to retrieve the FXML file "home.FXML"; it's failing at this line:

Parent root = FXMLLoader.load(getClass().getResource("/fxml/home.fxml"));

This is the rest of the class that loads the FXML.

public class Boorus extends Application{

public static void main(String[] args){
    launch(args);
}

public void start(Stage primaryStage) throws Exception{

    SQLboss.makeTables();
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/home.fxml"));
    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.setTitle("Boorus");
    primaryStage.show();


}
}

My project structure looks as pictured:

enter image description here

Pastebinned the Exception here: http://pastebin.com/rWkRuury

2

There are 2 answers

0
Roland On BEST ANSWER

The fxml file is being loaded. The problem is that it is broken.

There's something in it that doesn't work inside the jar. Unless you show us the fxml, nobody can help you. By the way, this would be the exception when the file isn't found:

java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at application.Main.start(Main.java:14)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$53/1018544248.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$45/1586270964.run(Unknown Source)

Yours however is:

Caused by: javafx.fxml.LoadException:
fxml/home.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)

It could be all kinds of things, like e. g. this one.

0
Rainer Schwarze On

It is likely that the .fxml file is not included in your JAR file.

In the steps where you build the JAR file, check that .fxml files are included and not excluded.

If you want to check the contents of your JAR file and are not sure how to: Rename the extension to .zip (it is actually a zip file) and open it.