0

For my current Project, I need a File path to different FXML files. I learned relatively quick that this can't be done with absolute paths, so I used MainApp.class.getResource() to the the relative path to my resource files. However, I can't get it to work. My code looks like this:

    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("../views/FXML/Root.fxml"));
            rootLayout = (BorderPane) loader.load();
            [...]

(In this example, to load the Root.FXML file one folder above my current one, in the FXML folder which is in itself in the views folder.

My folder structure looks like this (I left out a view files not necessary for this problem):

.
├── out
    ├── artifacts
        └── LagerGUI
            ├── LagerGUI.html
            ├── LagerGUI.jar
            └── LagerGUI.jnlp
    ├── logs
        └── log.txt
    └── production
        ├── Controllers
        ├── Logger
        ├── MainApp
        ├── META-INF
        ├── structs
        └── views


├── src
|   ├── MainApp
        └── MainApp.java
    └── views
        ├── Controllers
        └── FXML
            └──Root.FXML

When I run this in my IDE, everything works just perfect. However, when I try to Build Artifacts and run java -jar MyProject.jar from my terminal, I get the following:

[...]
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 MainApp.MainApp.initRootLayout(MainApp.java:226)
    at MainApp.MainApp.start(MainApp.java:213)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$106(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$119(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$117(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$118(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$450(GtkApplication.java:139)
    ... 1 more
Exception running application MainApp.MainApp

Which in javafx means that the location URL in loader.setLocation() couldn't be found. Any ideas?

Edit on this question being duplicate: Please note that the class.getResource way worked for the duplicate Question, however it does not in my case.

st0ck
  • 39
  • 1
  • 4
  • "Can't be done with absolute paths". Doesn't `"/views/FXML/Root.fxml"` work here? – James_D Jan 21 '18 at 15:16
  • You can also consider creating a "marker interface" in your `views/FXML` package: `package views.FXML ; public interface FXMLViews { }`. Then you could load the FXML with `FXMLViews.class.getResource("Root.fxml")`. – James_D Jan 21 '18 at 15:33
  • @James_D that is a fantastic Idea, thank you very much! – st0ck Jan 21 '18 at 16:00

0 Answers0