I am trying to include an fxml file in another one and deploy my application as a runnable jar.
The top-level fxml file is loaded like this:
URL location = Editor.class.getClassLoader().getResource("view/app.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
Parent root = fxmlLoader.load();
appController = fxmlLoader.getController();
app.fxml, which is located in "src/view" contains this line:
<fx:include fx:id="console" source="console.fxml" />
Running this in eclipse will work as expected, but running the exported .jar file will print:
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
view/app.fxml:92
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at de.hsa.dice.editor.Editor.start(Editor.java:49)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.net.MalformedURLException: Could not open InputStream for URL 'rsrc:console.fxml'
at org.eclipse.jdt.internal.jarinjarloader.RsrcURLConnection.getInputStream(RsrcURLConnection.java:49)
at java.base/java.net.URL.openStream(URL.java:1117)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2465)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:105)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 12 more
Note that the console.fxml file is in the same package as app.fxml (in both the IDE and the .jar file). That's why I also tried source="./console.fxml", but nothing changed.
I found the reason and a workaround for this issue.
The javafx.fxml.FXMLLoader class uses this URL constructor, when creating the FXMLLoader for included fxml files:
public URL(URL context, String spec)
using the location from the FXMLLoader(location) constructor as context and using the "source" from the include-element as spec.
So when loading my root fxml file with the path "view/app.fxml", the context will be the "view" package in my IDE, while it will be the root of the classpath in the exported .jar.
I tried creating the app-URL with
URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
URL location = new URL(classpath, "view/app.fxml");
but my context will not be used again when this is plugged into the same constructor again later by the FXMLLoader.
So the only workaround I could think of is to just put all fxml-files (at least those with an include tag) into the root of a source-folder. That way, it will be located directly at the classpath in any environment.