0

I have started doing a JavaFX project and I have followed the setup instructions from this question: IntelliJ can't recognize JavaFX 11 with OpenJDK 11 using VSCode. I am on a Macbook.

I got as far as the final part, where I was to set up thing with settings.json and launch.json. I followed the instructions as exactly as possible, but the error: Error: JavaFX runtime components are missing, and are required to run this application keeps showing up.

Here is what my settings.json and launch.json files look like:

settings.json

{
    "maven.view": "flat",
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "javafx-sdk-11.0.2/**/*.jar"
    ],
    "java.dependency.packagePresentation": "hierarchical"
}

launch.json

{
    "configurations": [
        {
            "type": "java",
            "name": "CodeLens (Launch) - Main",
            "request": "launch",
            "mainClass": "src.Main",
            "projectName": "<censored>"
        },
        {
            "type": "java",
            "name": "CodeLens (Launch) - Main",
            "request": "launch",
            "vmArgs": "--module-path <path_from_root_directory_through_desktop_to_folder_with_project>/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml",
            "mainClass": "Main",
            "projectName": "Main"
        }
    ]
}

I have replaced the actual path to the JavaFX SDK with: <path_from_root_directory_through_desktop_to_folder_with_project> for security.

The path itself is assuming the VSCode compiler starts at the root directory to find the JavaFX SDK (I've tried it assuming it starts in the project which is being run, but that still didn't work).

I have assigned "mainClass" and "projectName" to the file name of the java file containing all my code (minus the .java extension)

Here is the code for the java file I am using, if it is any help:

package src;

import javafx.scene.layout.Pane;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws FileNotFoundException {
        //declares a pane
        Pane pane = new Pane();
        
        //IMAGE ONE
        //creates a new imageView
        ImageView image1 = new ImageView(new Image(new FileInputStream("c1.gif")));
        
        //binds the image to the left of the center of the pane width
        image1.xProperty().bind((pane.widthProperty().divide(2)).subtract(36));
        
        //binds the image to the center of the pane height
        image1.yProperty().bind(pane.widthProperty().divide(2));
        
        //formats the image
        image1.setFitWidth(71);
        image1.setFitHeight(96);
        image1.setPreserveRatio(true);
        
        //adds the image
        pane.getChildren().add(image1);
        
        
        //IMAGE TWO
        //creates a new imageView
        ImageView image2 = new ImageView(new Image(new FileInputStream("c2.gif")));
        
        //binds the image to the center of the pane width
        image2.xProperty().bind(pane.widthProperty().divide(2));
        
        //binds the image to the center of the pane height
        image2.yProperty().bind(pane.widthProperty().divide(2));
        
        //formats the image
        image2.setFitWidth(71);
        image2.setFitHeight(96);
        image2.setPreserveRatio(true);
        
        //adds the image
        pane.getChildren().add(image2);
        
        
        //IMAGE THREE
        //creates a new imageView
        ImageView image3 = new ImageView(new Image(new FileInputStream("c3.gif")));
        
        //binds the image to the right of the center of the pane width
        image3.xProperty().bind((pane.widthProperty().divide(2)).add(36));
        
        //binds the image to the center of the pane height
        image3.yProperty().bind(pane.widthProperty().divide(2));
        
        //formats the image
        image3.setFitWidth(71);
        image3.setFitHeight(96);
        image3.setPreserveRatio(true);
        
        //adds the image
        pane.getChildren().add(image3);
        
        
        //SCENE SETUP
        //adds the pane with images to new scene
        Scene scene = new Scene(pane, 500, 500);
        
        //sets title of stage
        primaryStage.setTitle("DisplayImage");
        
        //displays the stage
        primaryStage.show();
        }
    
    public static void main(String []args) {
        launch(args);
        }
    }

Please clarify if I did something wrong.

Just to clarify, I am NOT asking if there is anything wrong with the code, I would like to know what I did wrong with using the JavaFX SDK

There might not be enough information, please let me know what other information I need to provide.

iNoob
  • 27
  • 8
  • Are you confident you've installed the jfx sdk correctly? Also note that a (better IMO) alternative to the jvm arguments is to add a module-info.java to your project with dependencies defined. – sprinter Oct 14 '20 at 23:35
  • Why are you referencing a tutorial about IntelliJ? https://stackoverflow.com/questions/54349894/javafx-11-with-vscode – SedJ601 Oct 15 '20 at 00:11
  • @sprinter I am not confident I installed it correctly. – iNoob Oct 16 '20 at 02:03
  • 1
    @Sedrick This was the closest I got when searching the web, and it got me quite close to getting it to work. – iNoob Oct 16 '20 at 02:03

1 Answers1

0

1.Check the installation and environment configuration.

Reference: Getting started with JavaFX

2.Check the setting java.configuration.runtimes, from which VS Code detects the runtime required for your project and choose the appropriate configuration.

Refernece: JDK for projects

If there's nothing achieved, install another jdk and have a try.

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22