【问题标题】:Include FXML throws Exception from .jar包含 FXML 从 .jar 抛出异常
【发布时间】:2019-04-21 05:54:51
【问题描述】:

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

顶层fxml文件是这样加载的:

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

app.fxml,位于“src/view”中包含这一行:

<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",但没有任何改变。

【问题讨论】:

  • 没人,真的吗?我认为这可能是一个常见的场景......因为在生产环境中还有人如何使用 include 标签?

标签: java url include fxml executable-jar


【解决方案1】:

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

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

public URL(URL context, String spec)

使用 FXMLLoader(location) 构造函数中的位置作为上下文,并使用包含元素中的“源”作为规范。

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

我尝试使用

创建应用程序 URL
URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
URL location = new URL(classpath, "view/app.fxml");

但是当它稍后被 FXMLLoader 再次插入同一个构造函数时,我的上下文将不会被再次使用。

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

【讨论】:

    【解决方案2】:

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

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

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

    除了 Eclipse 的导出工具之外,还有 more ways 用于打包 JavaFX 应用程序,尽管我还没有测试当 fxml 文件包含其他 fxml 文件时它们的行为。

    【讨论】:

    • 由于声誉不足,我无法评论现有问题/答案,不得不发布新答案。
    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2019-01-24
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多