【发布时间】:2017-10-10 15:19:53
【问题描述】:
我正在尝试从我在 Ideallij 中创建的 javafx 项目中导出 jar 文件。我通过在输出中添加我所有的 jar 库来为工件设置属性。然后,我构建工件并创建 jar 文件。但是,当我尝试使用 java - jar name.jar 从终端运行 jar 时,我收到以下错误:
应用程序启动方法中的异常,java.lang.reflect.InvocationTargetException
fxml 文件中的错误点。如何在压缩的jar文件中添加fxml文件?
FXML 加载:
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(EchoClient.class.getResource("name.fxml"));
Parent p = fxmlLoader.load();
Scene scene = new Scene(p);
primaryStage.setScene( scene );
primaryStage.setTitle("FX Echo Client");
primaryStage.setWidth( 320 );
primaryStage.setHeight(568);
primaryStage.show();
}
错误位于Parent p = fxmlLoader.load();。收到的错误如下:
编辑:经过一番研究,我得出的结论是,我的问题与following post 是理想的。因此,我从 FXML 文件 中删除了删除 fx:controller 属性,并且通常在没有应用程序功能的情况下调用 jar。如何将控制器功能添加回代码?
我的 fxml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="services.EchoClientController">
<children>
<VBox spacing="10.0" VBox.vgrow="SOMETIMES">
<children>
<Label text="Host" />
<TextField fx:id="tfHost" text="localhost" />
<Label text="Port" />
<TextField fx:id="tfPort" text="10000" />
<HBox spacing="4.0">
<children>
<Button fx:id="btnConnect" mnemonicParsing="false" onAction="#connect" text="Connect" />
<Button fx:id="btnDisconnect" mnemonicParsing="false" onAction="#disconnect" text="Disconnect" />
</children>
</HBox>
</children>
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</VBox>
<Separator prefWidth="200.0" />
<VBox spacing="10.0" VBox.vgrow="ALWAYS">
<children>
<Label text="Message To Send" />
<TextField fx:id="tfSend" />
<Button fx:id="btnSend" mnemonicParsing="false" onAction="#send" text="Send" />
<Label text="Message Received" />
<TextField fx:id="tfReceive" editable="false" />
</children>
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</VBox>
<Separator prefWidth="200.0" />
<VBox VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</VBox.margin>
<children>
<HBox fx:id="hboxStatus" spacing="10.0">
<children>
<ProgressBar fx:id="piStatus" prefWidth="150.0" progress="0.0" />
<Label fx:id="lblStatus" text="Label" />
</children>
</HBox>
</children>
</VBox>
</children>
</VBox>
【问题讨论】:
-
至少告诉我们你是如何通过代码加载fxml文件的。
-
我更新了代码。
-
嗯,你仍然需要向我们展示你的项目设置,你的文件所在的位置,你的包等等。
-
“fxml 文件中的错误点。” 如果是这样,则 fxml 文件存在问题,而不是将其添加到 jar 中...跨度>
-
代码正在运行,我正在正确加载 fxml 文件并描绘了整个场景。