Add fxml file to export jar file

I am trying to export a jar file from a javafx project that I created in ideallij. I made properties for artifacts by adding all my jar libraries to the output. Then I create an artifact and a jar file is created. However, when I try to start the jar from the terminal using java - jar name.jar, I get the following error:

An exception to the application launch method, java.lang.reflect.InvocationTargetException

Error in fxml file. How to add fxml file to compressed jar file?

FXML Download:

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();
}

The error is in Parent p = fxmlLoader.load();. The error getting am is as follows:

enter image description here

EDIT: , . , fx:controller FXML, , , . cam ?

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>
+4
2

, .fxml . , .jar , -, . , , fxml (, Java GUI), , .

+2

services.EchoClientController, :

@FXML
private void connect() {
}

@FXML
private void disconnect() {
}

@FXML
private void send() {
}

, :

@Override
public void initialize(URL url, ResourceBundle rb) {
    // Maybe constructor code here.
}    

FXMLLoader fxmlLoader = 
        new FXMLLoader(EchoClient.class.getResource("name.fxml"));
Parent p = fxmlLoader.load();

( )

Parent p = FXMLLoader.load(EchoClient.class.getResource("name.fxml"));
+2

Source: https://habr.com/ru/post/1687242/


All Articles