有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Include FXML从引发异常。罐子

我试图在另一个fxml文件中包含一个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”中的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

请注意,控制台。fxml文件与应用程序位于同一个包中。fxml(在IDE和.jar文件中)。 这就是为什么我也尝试了source=“./console.fxml”,但没有任何改变


共 (2) 个答案

  1. # 1 楼答案

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

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

    public URL(URL context, String spec)
    

    使用FXMLLoader(location)构造函数中的位置作为上下文,使用include元素中的“source”作为规范

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

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

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

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

    因此,我能想到的唯一解决办法就是将所有fxml文件(至少是那些带有include标记的文件)放到源文件夹的根目录中。这样,它将直接位于任何环境中的类路径上

  2. # 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文件时它们的行为