I am trying to export a jar file from a javafx project i have created in the ideallij. I did the properties for the artifacts, by adding all my jar libraries in the output. Then, I build the artifact and the jar file is created. However, when i am trying to run the jar from the terminal with java - jar name.jar
I am receiving the following error:
Exception in Application start method, java.lang.reflect.InvocationTargetException
The error points in the fxml file. How can I add the fxml file in the compressed jar file?
FXML loading:
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 located in Parent p = fxmlLoader.load();
.The error am receiving is the following:
EDIT: After a bit of research I came to the conclusion that my problem is ideantical with the following post. Thus I removed the remove the fx:controller
attribute from the FXML file and the jar was normally called without the functionality of the application though. How cam I add the controller functionality back to the code?
My fxml file is the following:
<?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>
2 Answers
Answers 1
I usually get this error when there is a typo in the .fxml file. I think the file itself flows to the .jar file, there is just something in there which cannot be resolved. You can prove that by deleting most of the fxml content temporary (make sure there will be no reference from the Java code to any missing GUI element) to see if the error disappears.
Answers 2
The error could only originate from services.EchoClientController
, missing:
@FXML private void connect() { } @FXML private void disconnect() { } @FXML private void send() { }
As it cannot be that simple, look at the constructor and consider using:
@Override public void initialize(URL url, ResourceBundle rb) { // Maybe constructor code here. }
Instead of
FXMLLoader fxmlLoader = new FXMLLoader(EchoClient.class.getResource("name.fxml")); Parent p = fxmlLoader.load();
I did (what should not make a difference)
Parent p = FXMLLoader.load(EchoClient.class.getResource("name.fxml"));
0 comments:
Post a Comment