代码之家  ›  专栏  ›  技术社区  ›  d3t0x

jar中的FXML控制器错误:位于javafx。fxml。FXMLLoader。constructLoadException构造加载异常

  •  0
  • d3t0x  · 技术社区  · 7 年前

    我正在将Maven Shade插件与Eclipse和JavaFX一起使用。Maven Shade创建了一个可运行的jar,允许我运行我的程序。该程序通过eclipse编译、运行并显示GUI,没有任何错误。maven构建运行时没有错误,并且创建了jar,当我尝试运行我的jar时,问题就出现了。

    我得到以下错误。

    我在这里读过几篇类似的文章,但没有一篇与我的设置相同。 Similar 这家伙把fxml文件移到了他的资源文件夹中,但我想试着保留我的项目MVC。还有其他修复方法吗?

    我的问题似乎是 fx:控制器= 主菜单。fxml

    错误:由:java引起。lang.IllegalStateException:未设置位置。

    错误

    javafx.fxml.LoadException: 
    file:/Users/bob/git/Corkscrew/Corkscrew/Corkscrew%20Server/target/Corkscrew-0.0.1-SNAPSHOT.jar!/io/ironbytes/corkscrew/views/MainMenu.fxml:10
    
        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at io.ironbytes.corkscrew.models.MainMenu.start(MainMenu.java:23)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at io.ironbytes.corkscrew.models.ConnectionsPane.<init>(ConnectionsPane.java:33)
        at io.ironbytes.corkscrew.controllers.MainMenuController.<init>(MainMenuController.java:32)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
        at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
        at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
        ... 14 more
    

    主菜单。Java语言

    package io.ironbytes.corkscrew.models;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class MainMenu extends Application {
        Parent root;
        Stage stage;
    
        @Override
        public void start(Stage primaryStage) {
    
            try {
                root = FXMLLoader.load(getClass().getClassLoader().getResource("io/ironbytes/corkscrew/views/MainMenu.fxml"));
    
                stage = primaryStage;
                stage.setTitle("Welcome");
    
                Scene scene = new Scene(root);
                stage.setScene(scene);
                stage.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    主菜单。fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.control.ToolBar?>
    <?import javafx.scene.image.Image?>
    <?import javafx.scene.image.ImageView?>
    <?import javafx.scene.layout.BorderPane?>
    
    <BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.ironbytes.corkscrew.controllers.MainMenuController">
       <left>
          <ToolBar orientation="VERTICAL" BorderPane.alignment="CENTER">
            <items>
              <Button mnemonicParsing="false" onAction="#onConnectionsBtnClick" prefHeight="27.0" prefWidth="82.0" text="Clients" />
                <Button mnemonicParsing="false" onAction="#onBuilderBtnClick" prefHeight="27.0" prefWidth="83.0" text="Builder" />
                <Button mnemonicParsing="false" prefHeight="27.0" prefWidth="83.0" text="Crypter" />
                <Button mnemonicParsing="false" onAction="#onCorkscrewBtnClick" text="Corkscrew" />
                <Button fx:id="listenBtn" mnemonicParsing="false" onAction="#onListenBtnClick" prefHeight="27.0" prefWidth="82.0" text="Listen" textFill="#e43333" />
                <TextField prefHeight="27.0" prefWidth="8.0" promptText="Port: 1337" />
            </items>
          </ToolBar>
       </left>
       <center>
          <ImageView fitHeight="255.0" fitWidth="348.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
             <image>
                <Image url="@../../../../img/logo.png" />
             </image>
          </ImageView>
       </center>
    </BorderPane>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   d3t0x    7 年前

    我终于找到了解决办法。几个小时后调试,玩pom。xml文件以及更改路径和文件结构,我意识到问题是jar无法加载或不喜欢fxml文件中的fx:controller。因此,我需要加载控制器并将其链接到fxml文件,而无需使用 fx:控制器=

    解决方案:

    去除 fx:contorller=“io.ironbytes.corkscrew.view.MainMenu.fxml”

    并更改控制器的加载方式:

        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setController(new MainMenuController());
            loader.setLocation(getClass().getResource("/io/ironbytes/corkscrew/views/MainMenu.fxml"));
            root = loader.load();
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }`