[使用Spring Boot执行时,应用程序启动方法中的JavaFX异常

问题描述 投票:1回答:1

我创建了一个简单的javaFX程序,并通过使该项目成为maven项目,向javaFX添加了spring boot。在添加spring boot之前,我没有任何错误,该应用程序运行正常。我在下面提供了完整的代码

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
    </parent>
    <groupId>groupId</groupId>
    <artifactId>RMI-DesktopClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>  
</project>

AlarmsystemApplication.java

    @Configuration
@SpringBootApplication
public class AlarmSystemApplication extends Application {

    private ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        this.applicationContext = SpringApplication.run(AlarmSystemApplication.class);

    }
    @Override
    public void stop() throws Exception {
        applicationContext.close();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Login.loadView(stage);
    }
}

Login.java

  public class Login {
    public static void loadView(Stage stage) {

        try {
            Parent view = FXMLLoader.load(Login.class.getResource("../../../../../resources/com.ui.views/Login.fxml"));
            stage.setScene(new Scene(view));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Login.fxml

    <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.alarmsystem.ui.views.Login">
   <children>
      <Label layoutX="137.0" layoutY="157.0" text="Hello JavaFX">
         <font>
            <Font size="59.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

我得到的错误

    Exception in Application start method
2020-04-23 22:33:12.134  INFO 7228 --- [lication Thread] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-04-23 22:33:12.136  INFO 7228 --- [lication Thread] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-04-23 22:33:12.139  INFO 7228 --- [lication Thread] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
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 com.alarmsystem.ui.views.Login.loadView(Login.java:16)
    at com.alarmsystem.ui.AlarmSystemApplication.start(AlarmSystemApplication.java:34)
    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 com.alarmsystem.ui.AlarmSystemApplication

Process finished with exit code 1

文件结构enter image description here

资源路径enter image description here

java spring-boot javafx pom.xml
1个回答
0
投票

我不确定这与Spring有关;您用于FXML的资源名称不是有效的资源名称(并且堆栈跟踪指示无法找到FXML文件是问题)。

特别是,资源名称中不能包含.,因此..com.ui.views均无效。

所以这里有两个问题:一个是资源名称不能与其中指定的“父目录”一起使用,另一个是您创建了文件夹(不是package )下的resources中带有非法的.字符。另请注意,resourcessource文件夹,在运行时不可用。

因此,首先,在名为com.ui.views的资源下创建一个package,并将FMXL放置在其中。我不使用IntelliJ,所以我不确定是否有执行此操作的选项,但是如果没有,则可以创建文件夹com,其子文件夹ui和该子文件夹[ C0],然后将FXML文件放置在views中。

然后,假设您使用的是Maven默认值,那么FXML的正确资源名称是

views

如果稍微进行重组,以使FXML文件和Parent view = FXMLLoader.load(Login.class.getResource("/com/ui/views/Login.fxml")); 类位于同一包中(即,在Login下创建一个com.alarmsystem.ui.views包,则可以这样做]]

resources

如果需要进一步排除故障,可以通过在Parent view = FXMLLoader.load(Login.class.getResource("Login.fxml")); 文件夹中查看资源的部署位置(因此可以在运行时

上找到它们)。
© www.soinside.com 2019 - 2024. All rights reserved.