我正在尝试使用 Maven 编写 JavaFx8 应用程序。我编写了一个简单的应用程序主类和一个 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$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 的新手,我已经遇到过这种错误,但这次我没有发现问题。
我的课程是: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 输出的类路径(这是拥有类路径的正确方法吗?),为此我在 fxml 文件包中编写了一个“Unused.java”类:
package org.aklal.todofx.tasks.view;
public class Unused {
public Unused(){
System.out.println("Unused\n\t" + this.getClass());
}
}
当我运行应用程序时,getClass 输出是:
没用过
class org.aklal.todofx.tasks.view
未使用的APP
class org.aklal.todofx.tasks.App
所以在我看来,我给 loader.setLocation 的路径(“view/RootLayout.fxml”)是正确的,不是吗?
我还尝试将根 fxml 文件(重命名为 TestRootLayout)放在主类的包中,我仍然有错误。
任何人都可以看到错误吗?
笔记
我已经编写了 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 系统库中
更新:
为了检查问题是否没有更普遍的原因,我用硬编码元素编写了主类:
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);
然后它工作