添加Maven支持后JavaFX项目出现问题(Application启动方法中的异常) [英] Problem with JavaFX project after adding Maven support (Exception in Application start method)

查看:217
本文介绍了添加Maven支持后JavaFX项目出现问题(Application启动方法中的异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JDK 1.8创建了一个JavaFX项目.正常工作.但是,如果我尝试添加Maven支持,则编译会失败.

I created a JavaFX project using JDK 1.8. Works correctly. But if I try to add Maven support, compilation fails.

首先,我尝试使用JavaFX 11在Java 11中执行此操作,但是效果是相同的.我花了两天的时间来查找为什么它不起作用,并花了很多小时浏览了stackoverflow.最终,我决定在Java 8上尝试使用它,但是它仍然没有解决我的问题.

Firstly, I tried to do it in Java 11 with JavaFX 11 but effect was the same. I spent two days on finding why it doesn't work, glancing at stackoverflow a lot of hours. Finally, I decided to try it on Java 8 but it still haven't solved my problem.

如果'sample.fxml'与Main或资源位于同一文件夹中,则两者没有区别,两者都编译失败.

There is no difference if 'sample.fxml' exists in the same folder as Main or in resources, compilation fails on both of them.

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生成,已多次更改):

There is Main method (generated by IntelliJ, changed multiple times):

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 下的任何资源文件在运行时都不会最终位于classpath/modulepath上.

You have your sample.fxml file in the wrong place. Maven (and Gradle) follow a standard directory layout by default. It expects source files to be under src/main/java and resources to be under src/main/resources. Any resource files under src/main/java do not end up on the classpath/modulepath at runtime.

您应该:

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


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


Note: You have a directory named src/main/java/resources. I'm assuming this is where you tried to put the FXML file and found it still didn't work. The reason is because Maven doesn't expect resources to be at that location (location indicates source files). Maven expects resources to be under src/main/resources.

但是,如果我尝试添加Maven支持,则编译会失败.

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

您发布的堆栈跟踪,为

The stack trace you posted, as fabian mentions, is not the result of a compilation error. It is a runtime error.

这篇关于添加Maven支持后JavaFX项目出现问题(Application启动方法中的异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆