简体   繁体   English

无法启动javaFX项目中的应用程序

[英]Failed to start application in javaFX project

after I added Maven dependecies to my JavaFX program and created a new package, called Algorithm wth two classes, my application fails to start. 在将Maven依赖项添加到我的JavaFX程序并创建了一个名为“带有两个类的算法”的新程序包之后,我的应用程序无法启动。

Following Exceptions are thrown: 引发以下异常:

Caused by: java.lang.RuntimeException: Exception in Application start method Caused by: java.lang.NullPointerException: Location is required. 原因:java.lang.RuntimeException:应用程序启动方法中的异常原因:java.lang.NullPointerException:必需的位置。

and so on... 等等...

I have tried: 我努力了:
JavaFX "Location is required." JavaFX“位置是必需的。” even though it is in the same package 即使在同一包装中
and
Exception in Application start method java.lang.reflect.InvocationTargetException 应用程序启动方法java.lang.reflect.InvocationTargetException中的异常

Both didn't work 两者都不起作用

In the image below is my project structure, any help is highly appreciated. 在下面的图像中是我的项目结构,非常感谢您的帮助。 It seems that sample.fxml is not valid... 似乎sample.fxml无效...

Edit: 编辑:
relocating the fxml file in resources directory didn't work either 在资源目录中重新定位fxml文件也不起作用

图片

I've just copied this code approximately, put it on my IntelliJ, and it worked like a charm. 我只是大致复制了此代码,并将其放在我的IntelliJ上,它就像一个魅力。 Maybe you're just missing the / on the name of the resource? 也许您只是缺少资源名称上的/

Here's the exact code I used: 这是我使用的确切代码:

package hello;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Hello extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent parent = FXMLLoader.load(Hello.class.getResource("/hello.fxml"));
        Scene scene = new Scene(p, 400, 400);
        primaryStage.setTitle("my app");
        primaryStage.setScene(scene);
        primaryStage.centerOnScreen();
        primaryStage.show();
    }

    public static void main(String[] args) throws IOException {
        launch(Hello.class);
    }
}

The FXML file: FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="hello.Hello"
            prefHeight="400.0" prefWidth="600.0">
    <VBox>
        <Text>Hello</Text>
    </VBox>
</AnchorPane>

Run with IntelliJ. 与IntelliJ一起运行。 Works. 作品。 Create a Jar, run with java -jar... also works. 创建一个用Java -jar运行的Jar,也可以。

Your IDE, IntelliJ, loads resources files from the classpath using pattern recognition to match the file extensions. 您的IDE IntelliJ使用模式识别来匹配文件扩展名,从而从类路径中加载资源文件。 By default, IntelliJ includes extensions like JPG, PNG, HTML, etc... 默认情况下,IntelliJ包括JPG,PNG,HTML等扩展名。

This means you need to manually specify the file extensions you want to compiler to find, for you that's FXML. 这意味着您需要为FXML手动指定要编译器查找的文件扩展名。

Here are some simple instructions on how to achieve this: https://www.jetbrains.com/help/idea/working-with-projects.html 以下是一些有关如何实现此目标的简单说明: https : //www.jetbrains.com/help/idea/working-with-projects.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM