繁体   English   中英

包含FXML从.jar引发异常

[英]Include FXML throws Exception from .jar

我试图在另一个文件中包含一个fxml文件,并将我的应用程序部署为可运行的jar。

顶级fxml文件的加载方式如下:

URL location = Editor.class.getClassLoader().getResource("view/app.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
Parent root = fxmlLoader.load();
appController = fxmlLoader.getController();

位于“ src / view”中的app.fxml包含以下行:

<fx:include fx:id="console" source="console.fxml" />

在eclipse中运行它可以按预期运行,但是运行导出的.jar文件将显示:

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

请注意,console.fxml文件与app.fxml在同一程序包中(在IDE和.jar文件中)。 这就是为什么我也尝试了source =“ ./ console.fxml”,但没有任何改变的原因。

我找到了此问题的原因和解决方法。

为包含的fxml文件创建FXMLLoader时,javafx.fxml.FXMLLoader类使用此URL构造函数:

public URL(URL context, String spec)

使用FXMLLoader(location)构造函数中的位置作为上下文,并使用include-element中的“源”作为规范。

因此,当使用路径“ view / app.fxml”加载我的根fxml文件时,上下文将是我IDE中的“ view”包,而它将是导出的.jar中类路径的根。

我尝试使用创建应用程序URL

URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
URL location = new URL(classpath, "view/app.fxml");

但是当以后由FXMLLoader再次将其插入相同的构造函数时,将不再使用我的上下文。

因此,我唯一能想到的解决方法是将所有fxml文件(至少包含include标签的文件)放入源文件夹的根目录中。 这样,它将在任何环境中直接位于类路径中。

要从Eclipse和作为可运行的jar文件成功启动JavaFX应用程序,可以尝试在app.fxml使用绝对URL:

<fx:include fx:id="console" source="/view/console.fxml" />

这样,也可以从Eclipse导出的可运行jar中加载fxml文件。

除了Eclipse的导出工具外,还有更多的方法来打包JavaFX应用程序,尽管我还没有测试当fxml文件包含其他fxml文件的内容时它们的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM