2017-10-10 222 views
4

我想從我在ideallij中創建的javafx項目導出jar文件。我通過在輸出中添加所有的jar庫來完成工件的屬性。然後,我構建了工件並創建了jar文件。在FXML文件在導出的jar文件中添加fxml文件

Exception in Application start method, java.lang.reflect.InvocationTargetException

錯誤點:然而,當我試圖用java - jar name.jar運行來自終端的罐子我收到以下錯誤。我怎樣才能在壓縮的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(); .The錯誤我收到如下:

enter image description here

編輯:後有點研究,我來到結論是我的問題與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>

+1

至少告訴我們如何通過代碼加載fxml文件。 – JKostikiadis

+0

我更新了代碼。 – konstantin

+1

那麼你仍然需要向我們展示你的項目設置文件的位置,你的包等。 – JKostikiadis

回答

2

時,有在.fxml文件錯字我通常會出現此錯誤。我認爲文件本身流向.jar文件,那裏只有一些無法解析的東西。您可以證明通過刪除大部分臨時fxml內容(確保沒有從Java代碼引用任何缺少的GUI元素)來查看錯誤是否消失。

+0

有沒有可能與fxml文件的路徑有關? – konstantin

+0

如果舞臺與FXML有正確的聯繫,那麼我不這麼認爲。嘗試我建議的解決方案,排除這種可能性。 – Macskasztorik

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"));