错误:缺少JavaFX运行时组件,运行此应用程序需要这些组件。与gradle一起运行项目

错误:缺少JavaFX运行时组件,运行此应用程序需要这些组件。与gradle一起运行项目,java,javafx,Java,Javafx,我正在intellij中构建一个java项目。我已经导入了gradle脚本,build.gradle文件可以成功构建。但是,当我运行java文件时,会出现以下错误:“缺少JavaFX运行时组件,运行此应用程序需要这些组件”。我真的不知道是什么问题。 这是我的Main.java文件: 这是发射器 这是Duke.java文件: import javafx.application.Application; import javafx.scene.Scene; import javafx.scene

我正在intellij中构建一个java项目。我已经导入了gradle脚本,build.gradle文件可以成功构建。但是,当我运行java文件时,会出现以下错误:“缺少JavaFX运行时组件,运行此应用程序需要这些组件”。我真的不知道是什么问题。 这是我的Main.java文件:

这是发射器

这是Duke.java文件:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static java.lang.Integer.parseInt;

public class Duke extends Application {

    private Storage storage;
    private TaskList tasks;
    private Ui ui;

    private ScrollPane scrollPane;
    private VBox dialogContainer;
    private TextField userInput;
    private Button sendButton;
    private Scene scene;
    private Image user = new Image(this.getClass().getResourceAsStream("images/DaUser.png"));
    private Image duke = new Image(this.getClass().getResourceAsStream("images/DaDuke.png"));

    @Override
    public void start(Stage stage) {
        //Step 1. Setting up required components

        //The container for the content of the chat to scroll.
        scrollPane = new ScrollPane();
        dialogContainer = new VBox();
        scrollPane.setContent(dialogContainer);

        userInput = new TextField();
        sendButton = new Button("Send");

        AnchorPane mainLayout = new AnchorPane();
        mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);

        scene = new Scene(mainLayout);

        stage.setScene(scene);
        stage.show();

        stage.setTitle("Duke");
        stage.setResizable(false);
        stage.setMinHeight(600.0);
        stage.setMinWidth(400.0);

        mainLayout.setPrefSize(400.0, 600.0);

        scrollPane.setPrefSize(385, 535);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

        scrollPane.setVvalue(1.0);
        scrollPane.setFitToWidth(true);

        // You will need to import `javafx.scene.layout.Region` for this.
        dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);

        userInput.setPrefWidth(325.0);

        sendButton.setPrefWidth(55.0);

        AnchorPane.setTopAnchor(scrollPane, 1.0);

        AnchorPane.setBottomAnchor(sendButton, 1.0);
        AnchorPane.setRightAnchor(sendButton, 1.0);

        AnchorPane.setLeftAnchor(userInput, 1.0);
        AnchorPane.setBottomAnchor(userInput, 1.0);

        sendButton.setOnMouseClicked((event) -> {
            handleUserInput();
        });

        userInput.setOnAction((event) -> {
            handleUserInput();
        });

        dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));

    }

    private void handleUserInput() {
        Label userText = new Label(userInput.getText());
        Label dukeText = new Label(getResponse(userInput.getText()));
        dialogContainer.getChildren().addAll(
                DialogBox.getUserDialog(userText.getText(), user),
                DialogBox.getDukeDialog(dukeText.getText(), duke)
        );
        userInput.clear();
    }

    public String getResponse(String input) {
        return "Duke heard: " + input;
    }

    public Duke(String filePath) {
        ui = new Ui();
        storage = new Storage(filePath);
        try {
            tasks = new TaskList(storage.load());
        } catch (DukeException e) {
            ui.showLoadingError();
            tasks = new TaskList();
        }
    }

    public Duke() {
        this("data/duke.txt");
    }

    public static void main(String[] args) {
        new Duke("data/duke.txt").run();
    }

    public void run() {
        ui.showWelcome();

        boolean isExit = false;

        while (!isExit) {
            try {
                String fullCommand = ui.readCommand();
                ui.showLine(); 
                Command c = Parser.parse(fullCommand);
                c.execute(tasks, ui, storage);
                isExit = c.isExit();
            } catch (DukeException e) {
                ui.showError(e.getMessage());
            } finally {
                ui.showLine();
            }
        }
        
    }
}
更新:我将lib文件夹导入到我的项目中,现在错误消失了。但是在我运行项目时发生了另一个错误,这是“由以下原因引起的:java.lang.NullPointerException:输入流不能为null”

根据您的说明,您使用的是java 13而不是java 11

JavaFX不是Java13中JDK的一部分,也不是Java11中的一部分。您可以从中获取JavaFX。然后需要将javafxjar文件添加到模块路径中,因为由于jdk9,java使用模块

与您的问题无关,但“系统”属性包含有关java版本的详细信息,例如

System.getProperty(“java.runtime.version”);

您使用什么JRE/JDK?大多数OpenJRE无法运行javafx应用程序。我使用的是Java11。我不确定它是否与Duke.java文件有关。实际上,我只是从另一个基本javafx教程项目复制粘贴了一些文件,该项目运行正常。在将粘贴复制到我自己的项目时,我没有更改Duke文件,因为我不知道如何更改它以适应javafx,所以我不知道这是否是问题所在。在Duke类中还有一个主要功能。我需要改变吗?也许看看。可能没有找到图像资源,但您最好就此开始一个新问题。