从jar启动我的项目时,我无法加载fxml文件(jar是用maven生成的)。忽略System.outs只是为了快速调试。
我使用以下代码(此代码从类TabPaneTest中调用):
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TabPaneView.fxml"));
这是相应控制器的初始化方法:
@FXML
public void initialize() {
this.tabPane1.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
this.loginTab.setClosable(false);
this.loginTab.setText("Login");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/ConnectView.fxml"));
//Funktionierende jar
//FXMLLoader fxmlLoader = new FXMLLoader(new URL("rsrc:resources/de/statistik_nord/klr/view/EingabeView.fxml"));
//System.out.println(getClass().getClassLoader().getResourceAsStream("resources/de/statistik_nord/klr/view/EingabeView.fxml"));
fxmlLoader.setController(new ConnectController());
Parent root = null;
//Parent root = FXMLLoader.load(getClass().getResource("view/ConnectView.fxml"));
try {
root = (Parent) fxmlLoader.load(); //This is TabPaneController.java:52, which is mentioned in the stacktrace
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.loginTab.setContent(root);
dbc.bind(((ConnectController) fxmlLoader.getController()).getDbcProperty());
this.dbc.addListener(new ChangeListener<DBConnection>() {
@Override
public void changed(ObservableValue<? extends DBConnection> observable, DBConnection oldValue, DBConnection newValue) {
// TODO Auto-generated method stub
{
if(newValue != null) {
System.out.println(dbc + "new");
ec = new EingabeController(dbc.getValue());
changeExistingTab(loginTab, "Eingabe", "../view/EingabeView.fxml", ec, (ActionEvent e) -> {
{
if (e.getTarget() instanceof Button) {
switch(((Button) e.getTarget()).getId()) {
case "buttonUebersKTR":
openNewTab(new Tab(), "Uebersicht Kostentraeger", "../view/Übersicht_KTRView.fxml", new Uebersicht_KTRController(dbc.getValue(), ec.getChosenPersonal()));
break;
case "buttonPersonalKTR":
openNewTab(new Tab(), "Personalübersicht für: ", "../view/Personal_KTR_UebersView.fxml", new Personal_KTR_UebersController(dbc.getValue(), ec.getChosenKTR()));
break;
case "buttonUnvollstaendig":
openNewTab(new Tab(), "Unvollstaendig", "../view/UnvollstaendigView.fxml", new UnvollstaendigController(dbc.getValue()));
break;
case "buttonLogOut":
dbc.removeListener(this);
tabPane1.getTabs().removeIf(tab -> !(tab.getText().equals("Eingabe")));
initialize();
System.out.println(dbc + "case");
break;
}
} else {
System.out.println("Event!!!!");
}
}
});
}
}
}
});
}
罐子中的目录如下:
jar directories jar directories 2
我收到以下错误:
Caused by javafx.fxml.LoadException: File:/....../[...].jar!/de/statistik_nord/klr/TabPaneView.fxml
我还打印了路径并获得了它(当取出罐子时):
System.out.println(getClass().getResource("TabPaneView.fxml"));
jar:file:/...../[...].jar!/de/statistik_nord_klr/TabPaneView.fxml
从eclipse启动程序工作正常,并且具有基本相同的包装:
编辑:
Stacktrace:
/* This is sys.out.print output I do myself
TEEEEEEEEEEEEST
jar:file:/C:/Users/WollscPh/eclipse- workspace/klr_fx_test/target/klr_fx_test-0.0.1-SNAPSHOT.jar!/de/statistik_nord/klr/view/TabPaneView.fxml
TEEEEEEEEEEEEST
*/
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs (LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/WollscPh/eclipse-workspace/klr_fx_test/target/klr_fx_test-0.0.1-SNAPSHOT.jar!/de/statistik_nord/klr/TabPaneView.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at de.statistik_nord.klr.TabPaneTest.start(TabPaneTest.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 12 more
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at de.statistik_nord.klr.controller.TabPaneController.initialize(TabPaneController.java:52)
... 22 more
Exception running application de.statistik_nord.klr.TabPaneTest
fxml文件(TabPaneView,已加载到TabPaneTest.java主类中):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<center>
</center>
<center>
<TabPane fx:id="tabPane1" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab fx:id="loginTab" text="Untitled Tab 1" />
</tabs>
</TabPane>
</center>
</BorderPane>
fxml文件(ConnectView,这是第二个加载的fxml,它是从TabPaneController的initialize方法加载的)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<left>
<TextField id="text1" fx:id="textField1" prefHeight="48.0" prefWidth="149.0" promptText="User" BorderPane.alignment="CENTER" />
</left>
<right>
<TextField id="text2" fx:id="textField2" prefHeight="48.0" prefWidth="149.0" promptText="Passwort" BorderPane.alignment="CENTER" />
</right>
<center>
<Button fx:id="button1" mnemonicParsing="false" text="LogIn" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
以防万一,虽然实际上并没有发生任何事情,但如果Ill也为ConnectView包含了相应的Controller,
@FXML
public void initialize() {
// TODO Auto-generated method stub
button1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
dbc.set(new DBConnection(textField1.getText(), textField2.getText()));
}
});
}
答案 0 :(得分:0)
错误是在我使用的相对路径中使用了..
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/ConnectView.fxml"));
它存在于普通文件系统中,但不存在于jar中,这解释了为什么我从eclipse启动它但不能从可执行jar启动时起作用的原因。 解决方案如下:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("de/statistik_nord/klr/view/ConnectView.fxml"));
作为参考,ConnectView.fxml的路径是: MyJarName.jar / de / statistik_nord / klar / view / ConnectView.fxml。