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.