java - 添加 Maven 支持后 JavaFX 项目出现问题(应用程序启动方法异常)

标签 java maven exception javafx fxml

我使用 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)遵循 standard directory layout默认情况下。它希望源文件位于 src/main/java 下,资源位于 src/main/resources 下。 src/main/java 下的任何资源文件在运行时都不会出现在类路径/模块路径中。

你应该:

  1. sample.fxml 移动到 src/main/resources。或者,
    • 这需要将您的代码更改为 getResource("/sample.fxml")
  2. sample.fxml 移动到 src/main/resources/sample
    • 使用此选项,您可以保持代码不变。

注意:您有一个名为 src/main/java/resources 的目录。我假设这是您尝试放置 FXML 文件的地方,但发现它仍然不起作用。原因是因为 Maven 不希望资源位于该位置(位置表示源文件)。 Maven 期望资源位于 src/main/resources 下。


But if I try to add Maven support, compilation fails.

您发布的堆栈跟踪,如 fabian mentions , 不是编译错误的结果。这是一个运行时错误。

关于java - 添加 Maven 支持后 JavaFX 项目出现问题(应用程序启动方法异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53951676/

相关文章:

java - 如何在 Android 的 ListView(或 ListActivity)中设置单元格的背景颜色或图像?

java - Canvas3D 未出现在 Swing 窗口中

java - 使用 @ContextConfiguration 运行 spring 集成测试时,应用程序上下文为 null

java - 在 App Engine Standard (Java 8) 中使用 Spring Boot Web 应用程序时出现空白页面

java - 从旧 (1.x) 原型(prototype)创建的 Maven 项目

Java - 应该在哪里以及如何使用异常?

java - 记录时异常有什么好处?

Java JScrollPane 有时无法成功调整大小

c# - 白鲸记的异常(exception)

java - 正则表达式匹配任意位置包含 ()- 的电话号码