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

时间:2015-01-30 10:31:01

标签: eclipse maven javafx-8 fxmlloader

我正在尝试使用Maven编写JavaFx8应用程序。我写了一个简单的应用程序主类和一个fxml文件(一个什么都不做的root 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$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/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$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$56/1712616138.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1268447657.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$52/511893999.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/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$45(GtkApplication.java:126)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/584634336.run(Unknown Source)
    ... 1 more
Exception running application org.aklal.todofx.tasks.App

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

enter image description here

我的课程是: 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>

我检查了classpathes打印出getClass输出(这是使用classpath的正确方法吗?),为此我编写了一个&#34; Unused.java&#34; fxml文件包中的类:

package org.aklal.todofx.tasks.view;

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

}

当我运行App时,getClass输出为:

  

未使用的

     
   class org.aklal.todofx.tasks.view
  
     

未使用的APP

     
   class org.aklal.todofx.tasks.App
  

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

我还尝试将根fxml文件(重命名为TestRootLayout)放在主类&#39;包,我还有错误。

有人能看到错误吗?

注意

我已经编写了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中:导入 - &gt;现有项目进入工作区

fxrt.jar包含在JRE系统库

更新

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

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

然后它起作用

2 个答案:

答案 0 :(得分:8)

  

现在我创建的jar没有fxml文件。我想我做了一个   错误,我会再试一次让你知道

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

的子目录中添加FXML文件

如果你致电App.class.getResource("view/RootLayout.fxml"),那么FXML文件必须位于以下目录:src/main/resources/mypackage/view,其中mypackage是App类的包。

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

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

答案 1 :(得分:0)

正如Puce所说,如果你打电话给App.class.getResource("view/RootLayout.fxml"),那么FXML文件必须在以下目录中:src/main/resources/mypackage/view,其中mypackage是App类的包。

所以,而不是使用App.class

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

尝试ClassLoader

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