JavaFX 和 maven:NullPointerException:需要位置 [英] JavaFX and maven: NullPointerException: Location is required
问题描述
我一直在尝试使用 JavaFX 设置 Maven.尽管我对 Maven 和 JavaFX 没有经验,但我没想到它会是一个如此大的挑战.我的 Java 知识非常扎实(包括 Swing),没想到设置起来会这么困难.
I have been trying to get Maven set up with JavaFX. Even though I was unexperienced with Maven and JavaFX, I didn't expect it to be so much of a challenge. My Java knowledge is pretty solid (including Swing), and didn't expect to have this much difficulty getting it set up.
我从 IntelliJ 13.0 Community Edition 提供的 JavaFX 项目开始.我的Main类中的代码比较少:
I started with a JavaFX project supplied by IntelliJ 13.0 Community Edition. The code in my Main class is relatively small:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
//getClass().getResource("../../resources/sample.fxml");
Parent root = FXMLLoader.load(getClass().getResource("../sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
而且我的 pom.xml
也不是太大:
and my pom.xml
isn't too big either:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample</groupId>
<artifactId>JavaFXDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<organization>
<name>RubberDucky</name>
</organization>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<mainClass>sample.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2.2</version>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
<scope>system</scope>
</dependency>
</dependencies>
</project>
我正在使用 JavaFX Maven 插件 来构建应用程序.运行 mvn clean jfx:jar
后一切正常,所有构建成功.但是当我尝试运行该应用程序时,出现以下错误:
I'm using the JavaFX Maven Plugin to build the application. After running mvn clean jfx:jar
everything seems fine, all builds succeed. But when I try to run the application I get the following error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at sample.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
... 1 more
经过一些粗略的调试后,问题似乎出在我加载文件的路径上.将 sample.fxml
硬编码到我硬盘上的某个位置后,应用程序运行没有任何问题.从 IntelliJ 运行应用程序时,当前设置(如上所示)也是如此.
After some rough debugging the trouble seems to be in the path I am loading my files from. After hardcoding the sample.fxml
to a place on my hard drive, the application runs without any problems. Same goes for the current setup (seen above) when running the application from IntelliJ.
我觉得我已经用尽了所有资源(包括 StackOverflow,一些非常相似的错误),但我就是无法弄清楚到底是哪里出了问题.
I feel like I have exhausted every resource (including StackOverflow, some very similar errors) but I just can't figure out what exactly is wrong.
推荐答案
确保您的 sample.fxml
位于 src/main/resources/
目录(或子目录).然后你可以像这样访问文件:
Make sure that your sample.fxml
is in the src/main/resources/
directory (or a subdirectory). Then you can access the file like this:
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));
说明:在编译阶段,所有资源和类都被复制到 target/classes/
.因此,您的 fxml 文件位于此目录中,而您的类位于关于其包名称的子目录中.如果您调用 getClass().getResource("sample.fxml");
该文件将相对于该目录的类文件进行搜索:target/classes/sample/
代码>.
Explanation:
During the compile phase all resources and classes get copied to target/classes/
. So your fxml file resides in this directory and your class in a subdirectory regarding its package name. If you call getClass().getResource("sample.fxml");
the file will be searched relative to the class file which will be this directory: target/classes/sample/
.
在类加载器上调用 .getResource()
将相对搜索路径设置为 target/classes/
,从而找到您的文件.
Calling .getResource()
on the classloader sets the relative search path to target/classes/
and therefore your file gets found.
附言你也可以写:
Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
这篇关于JavaFX 和 maven:NullPointerException:需要位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!