[英]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.