1

I am building a java project in intellij. I alreadly imported the gradle script and build.gradle file can be successfully built. However, when I ran my java files, this error occurs: "JavaFX runtime components are missing, and are required to run this application". I am really not sure what is the issue. This is my Main.java file: enter image description here

This is the launcher enter image description here

and this is the Duke.java files:

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();
            }
        }
        
    }
}

update: I imported the lib folder into my project and now the error is gone. But another error occurs when I ran the project, which is "Caused by: java.lang.NullPointerException: Input stream must not be null"

Roy Xu
  • 197
  • 1
  • 3
  • 14
  • What JRE / JDK do you use? Most OpenJREs can't run javafx applications. – Tobias Aug 31 '20 at 06:05
  • I am using java 11. – Roy Xu Aug 31 '20 at 06:11
  • I am not sure if it is about Duke.java file. I actually just copy paste some files from another basic javafx tutorial project, and that project runs properly. When copying pasting to my own project, I didn't change the Duke file because I am not sure how to change it to adapt to the javafx so I don't know if it is the problem. – Roy Xu Aug 31 '20 at 06:15
  • and in the Duke class there is also a main funtion. do I need to change that? – Roy Xu Aug 31 '20 at 06:17
  • But what kind of java 11 JRE? [openJDK](https://openjdk.java.net/), [AdoptOpenJDK](https://adoptopenjdk.net/installation.html#), [OracleJRE](https://www.java.com/en/download/)? The Duke class should be ok. If this class would be the problem that would lead to a different exception. – Tobias Aug 31 '20 at 06:19
  • This is the what I get from command prompt: java 13.0.2 2020-01-14 Java(TM) SE Runtime Environment (build 13.0.2+8) Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing) – Roy Xu Aug 31 '20 at 06:22
  • I think it is oraclejre – Roy Xu Aug 31 '20 at 06:23
  • Oracle's jre usually works fine with javafx (at least when I tried it). In this case I don't know what the problem might be. But there are many questions (and answers) on similar problems on SO that you can check out: https://stackoverflow.com/search?q=%5Bjavafx%5D+runtime+components+missing – Tobias Aug 31 '20 at 06:29
  • so just now I imported the lib folder into my project and now the error is gone. But another error occurs when I ran the project, which is "Caused by: java.lang.NullPointerException: Input stream must not be null" – Roy Xu Aug 31 '20 at 06:42
  • 1
    Maybe have a look at [this question](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510#218510). Probably the image resources were not found, but you better start a new question on this. – Tobias Aug 31 '20 at 06:48

1 Answers1

1

According to your comment you are using Java 13 and not Java 11.

JavaFX is not part of the JDK in Java 13 and also not in Java 11. You can get JavaFX from openjfx.io. Then you need to add the JavaFX JAR files to your module path because since JDK 9, java uses modules.

Unrelated to your question, but the "system" properties contain details about the java version, for example...

System.getProperty("java.runtime.version");
Abra
  • 19,142
  • 7
  • 29
  • 41