java - 添加 Maven 支持后 JavaFX 项目出现问题(应用程序启动方法中的异常)
问题描述
我使用 JDK 1.8 创建了一个 JavaFX 项目。工作正常。但是如果我尝试添加 Maven 支持,编译会失败。
首先,我尝试使用 JavaFX 11 在 Java 11 中执行此操作,但效果相同。我花了两天时间寻找它为什么不起作用,看了很多小时的stackoverflow。最后,我决定在 Java 8 上尝试一下,但它仍然没有解决我的问题。
如果“sample.fxml”与 Main 或资源存在于同一文件夹中,则没有区别,编译都失败。
SDK 设置正确。
控制台日志:
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:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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:498)
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:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 sample.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
pom.xml:
<?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>pl.wojciech</groupId>
<artifactId>javafx11</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>sample.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
有 Main 方法(由 IntelliJ 生成,多次更改):
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
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);
}
}
还有我的项目结构:
javafx8 [
.idea {}
src
{
main
java
sample
Controller
Main
sample.fxml
resources
}
test {}
target {}
javafx8.iml
pom.xml
]
解决方案
你的sample.fxml
文件放错地方了。Maven(和 Gradle)默认遵循标准的目录布局。它期望源文件在下面src/main/java
,资源在src/main/resources
. 运行时下的任何资源文件src/main/java
都不会出现在类路径/模块路径上。
您应该:
- 移至。
sample.fxml
_src/main/resources
或者,- 这需要将您的代码更改为
getResource("/sample.fxml")
.
- 这需要将您的代码更改为
- 移至。
sample.fxml
_src/main/resources/sample
- 使用此选项,您可以保持代码不变。
注意:您有一个名为src/main/java/resources
. 我假设这是您尝试放置 FXML 文件并发现它仍然无法正常工作的地方。原因是因为 Maven 不希望资源位于该位置(位置表示源文件)。Maven 期望资源在src/main/resources
.
但是如果我尝试添加 Maven 支持,编译会失败。
正如fabian 提到的,您发布的堆栈跟踪不是编译错误的结果。这是一个运行时错误。
推荐阅读
- svg - SVG 在网页上工作,但不是作为嵌入式 .svg 文件
- java - 该项目未构建,因为它的构建路径不完整 - Eclipse Oxygen
- java - java - 如何在java中复制数组值而不是引用?
- gcc - 理解汇编中的pop指令
- android - videoview 中的 xamarin.android 视频流
- highcharts - 完成重绘后的 Highcharts 事件处理程序
- django - 计算每个月的平均值
- python - 如何在 pandas.cut 中打印类别?
- c# - 从 c# 调用时,Yelp Fusion API v3 返回 401 Unauthorized with TOKEN_MISSING 错误
- r - 蒙特卡洛积分的错误答案