0

In order to learn JavaFX I created my first project using IntelliJ IDEA and exported the project as an Eclipse project. Everything is working well when I run the project in those two IDEs. Unfortunately, after building the project and trying to run the .jar or the .exe file I get an exception and I think that the problem is in the FXMLLoader which is unable to find the fxml files.

public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("../view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
            // Give the controller access to the main app.
            RootLayoutController rootLayoutController = loader.getController();
            rootLayoutController.setMain(this);
        } catch (IOException e) {
            showExceptionDialog(e);
        }
        // Try to load last opened person file.
        File file = getPersonFilePath();
        if (file != null) {
            loadPersonDataFromFile(file);
        }
    }

Project hierarchy

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at com.melkojji.controller.Main.initRootLayout(Unknown Source)
        at com.melkojji.controller.Main.start(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more
Exception running application com.melkojji.controller.Main
Mustapha El Kojji
  • 757
  • 1
  • 5
  • 7

1 Answers1

0

I don't think your fxml files are being packaged with the jar. You should use a resources folder. Go to your projects settings and set the module's resource folder.

  • Where should I add or find this `resources` file.I have already a `resources` file in the build directory. Sould I use it? Should I edit the path in the code ? – Mustapha El Kojji Jan 28 '17 at 23:25
  • @MustaphaELKOJJI Check the content of the jar file before you restructure the project. I don't think this is the problem (or at least not the whole problem). – James_D Jan 28 '17 at 23:27
  • https://www.mkyong.com/java/java-read-a-file-from-resources-folder/ – Oliver Yasuna Jan 28 '17 at 23:44
  • You don't *have* to put non-Java resources in a separate folder, all you have to do is make sure they are deployed when you build the project. (I never use a resources folder, I just find it more convenient to have the FXML file in the same folder as the corresponding controller source code.) You do have to use proper resource names, though, and `..` is not a valid resource name. – James_D Jan 28 '17 at 23:52
  • Even after changing replacing `../view/RootLayout.fxml` by `/com/melkojji/view/RootLayout.fxml`, the problem is the same. – Mustapha El Kojji Jan 29 '17 at 00:33
  • @MustaphaELKOJJI Then you should check the fxml file is being deployed to the jar file, as OliverYasuna implies in this answer. You can easily check the content of the jar file (`jar tf myjarfile.jar` from the command line) and see if the fxml file is in there, and if it's in the correct location. – James_D Jan 29 '17 at 00:59
  • Yes the fxml files are deployed to the jar file. – Mustapha El Kojji Jan 29 '17 at 01:13
  • Thank you so much for your great discussion and help. I forgot to replace the `..` in an other function and the error was the same. Now it is working. – Mustapha El Kojji Jan 29 '17 at 01:48