2017-10-10 65 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"));