JavaFx8 (+Maven): 加载fxml文件时出错

JavaFx8 (+Maven): error when loading the fxml file

我正在尝试使用 Maven 编写 JavaFx8 应用程序。我写了一个简单的应用程序 main class 和一个 fxml 文件(一个什么都不做的根 fxml 文件)。

当我尝试加载 fxml 根文件时出现错误“位置未设置”:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication7(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda/99550389.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2428)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
    at org.aklal.todofx.tasks.App.initRootLayout(App.java:58)
    at org.aklal.todofx.tasks.App.start(App.java:31)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1712616138.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1268447657.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda/511893999.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1851691492.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:126)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda/584634336.run(Unknown Source)
    ... 1 more
Exception running application org.aklal.todofx.tasks.App

我不是 JavaFx8 的新手,我已经遇到过这种错误,但是这次我没有找到问题。

我的class是: App.java

package org.aklal.todofx.tasks;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;


public class App extends Application {

        private Stage primaryStage;
        private BorderPane rootLayout;

        public App() {
                System.out.println("TEST");
        }

        @Override
        public void start(Stage primaryStage) {
                this.primaryStage = primaryStage;
                this.primaryStage.setTitle("TEST App (JavaFx with Maven)");

                initRootLayout();
        }

        public void initRootLayout() {
                try {
                        //to check classpaths 
                        Unused un = new Unused();
                        System.out.println("TESTAPP\n\t" + this.getClass());

                        // Load root layout from fxml file.
                        FXMLLoader loader = new FXMLLoader();
                        loader.setLocation(App.class.getResource("view/RootLayout.fxml"));
                        //loader.setLocation(App.class.getResource("TestRootLayout.fxml"));
                        rootLayout = (BorderPane) loader.load();

                        // Show the scene containing the root layout.
                        Scene scene = new Scene(rootLayout);
                        primaryStage.setScene(scene);
                        primaryStage.show();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }


        public Stage getPrimaryStage() {
                return primaryStage;
        }

        public static void main(String[] args) {
                launch(args);
        }
}

RootLayout.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>


<BorderPane prefHeight="500.0" prefWidth="1024.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
        <!-- TODO Add Nodes -->
</BorderPane>

我检查了打印出 getClass 输出的 class 路径(这是获得 class 路径的正确方法吗?),为此我写了一个“Unused.java”class在fxml文件包中:

package org.aklal.todofx.tasks.view;

public class Unused {
        public Unused(){
                System.out.println("Unused\n\t" + this.getClass());
        }

}

当我 运行 应用程序时,getClass 输出是:

Unused

   class org.aklal.todofx.tasks.view

未使用的应用程序

   class org.aklal.todofx.tasks.App

所以在我看来我给 loader.setLocation 的路径 ("view/RootLayout.fxml") 是正确的,不是吗?

我也试过把根fxml文件(改名为TestRootLayout)放到主class包里,还是报错。

有人能看出错误吗?

备注

我已经编写了 JavaFx 应用程序,但我从未使用 Maven 来完成它,该项目的目的是使用 Maven 设置一个 JavaFx8 项目。我认为我的问题不是来自 Maven,但我给了你我为设置项目所做的命令,也许有问题: 我执行了命令:

mvn archetype:generate -DgroupId=org.aklal -DartifactId=javafx-with-maven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

我修改了pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.aklal</groupId>
  <artifactId>javafx-with-maven</artifactId>
  
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  
  <name>javafx-with-maven</name>
  
  <url>http://maven.apache.org</url>
  
    <dependencies>
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <finalName>JavaFXSimpleApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>        
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mainClass>org.aklal.todofx.tasks.App</mainClass>
                </configuration>
            </plugin>
        </plugins>    
    </build>
</project>

然后:

mvn eclipse:eclipse

在 Eclipse 中:导入 -> 现有项目到工作区

fxrt.jar 包含在 JRE 系统库中

更新:

为了检查问题是否有更普遍的原因,我用硬编码元素编写了主要 class:

public class App extends Application {

    public static void main(String args[]) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Button bouton = new Button("Click");
        bouton.setOnAction(e -> System.out.println("Clicked!"));
        StackPane root = new StackPane();
        root.getChildren().add(bouton);
        stage.setScene(new Scene(root));
        stage.setWidth(300);
        stage.setHeight(300);
        stage.setTitle("JavaFx8 with Maven");
        stage.show();
    }
}

它有效,所以我想一切正常。问题仍然存在:为什么 setLocation 不起作用?

更新:

肯定是fxml文件的路径有问题。如果我改变:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(App.class.getResource("view/RootLayout.fxml"));

与:

String pathToFxml = "absolute_path/RootLayout.fxml";
URL fxmlUrl = new File(pathToFxml).toURI().toURL();
loader.setLocation(fxmlUrl);

然后就可以了

for now the jar I created did not have fxml files. I guess I made a mistake, I will give one more try and let you know

您需要在 src/main/resources 而非 src/main/java

的子目录中添加 FXML 文件

如果您调用 App.class.getResource("view/RootLayout.fxml"),则 FXML 文件必须位于目录中:src/main/resources/mypackage/view 其中 mypackage 是 class 应用程序的包。

如果您使用前导斜杠调用它:App.class.getResource("/view/RootLayout.fxml") 那么 FXML 文件必须位于目录中:src/main/resources/view

仔细检查 FXML 文件是否位于 JAR 文件中的预期位置。

正如 Puce 所说,如果您调用 App.class.getResource("view/RootLayout.fxml"),那么 FXML 文件必须位于目录中:src/main/resources/mypackage/view 其中 mypackage 是 class 应用程序的包。

所以,而不是使用 App.class

FXMLLoader loader = new FXMLLoader(); 
loader.setLocation(App.class.getResource("view/RootLayout.fxml"));

试试类加载器

FXMLLoader loader = new FXMLLoader(); 
loader.setLocation(ClassLoader.getSystemResource("view/RootLayout.fxml"));