我正在尝试从我在 Ideallij 中创建的 javafx 项目导出 jar 文件。我通过在输出中添加所有 jar 库来设置工件的属性。然后,我构建工件并创建 jar 文件。但是,当我尝试使用 java - jar name.jar
从终端运行 jar 时,我收到以下错误:
Exception in Application start method, 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。如何将 Controller 功能添加回代码中?
我的 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 文件中有拼写错误时,我通常会收到此错误。我认为文件本身流向 .jar 文件,其中只是有一些无法解析的内容。您可以通过临时删除大部分 fxml 内容(确保 Java 代码不会引用任何丢失的 GUI 元素)来证明这一点,看看错误是否消失。
关于java - 在导出的jar文件中添加fxml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46670377/