skip to Main Content

I am learning JavaFX using VScode, but I am continuously getting an error message stating, "Error: JavaFX runtime components are missing and are required to run this application", despite having entered vmArgs in launch.json and adding JavaFX to the referenced library as shown in multiple tutorial videos. My vmArgs look something like this:

"vmArgs": "--module-path C:/.../Desktop/javafx-sdk-20.0.1/lib --add-modules javafx.controls,javafx.fxml", 

I have tried multiple vmArgs, including having the JavaFX stored in the Download folder, but the error persists. I suspect that there may be an issue with my Configure Java Runtime, which appears to be empty, or something related to JAVA_HOME. Can someone help me resolve this issue? Thank you in advance.

2

Answers


  1. You better use the Java11 and above versions. And vmArgs is configured in launch.json, you need to use Run and Debug to debug, or Start Debugging or Run Without Debugging under the Run menu.

    enter image description here

    JavaFX and Visual Studio Code

    Login or Signup to reply.
  2. The standard solution is:

    I recommend that (if you really want to use Visual Studio), not what is in the rest of this answer.


    An alternate solution is to use a JDK which includes JavaFX.

    These instructions worked for me. I make no guarantee they will work for you or continue to work without modification in the future. I also do not offer support for these instructions (so don’t ask), they are presented "as-is".


    Visual Studio Code and Azul Zulu "JDK FX" distribution, which includes JavaFX

    Download and install Azul Zulu "JDK FX" for your OS and architecture.

    Here is a link to an example download for Java 20, OS X, x64, JDK FX package, you can go to the link and change the settings for your system, then download and run the installer (I choose a .dmg installer for Mac).

    That will install the JDK "somewhere" on your system. On a Mac you can find out where by typing:

    /usr/libexec/java_home -V
    

    That shows me:

    20.0.1 (x86_64) "Azul Systems, Inc." - "Zulu 20.30.11" /Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home
    

    So now I know where the JDK is installed.

    Then to setup the JDK in Visual Studio, go to "Java: Getting Started" on the Visual Studio site:

    Choose "Install the Coding Pack for Java – macOS" (use the Windows link instead if that is your OS).

    Once the VSCode installation is done, set up Zulu as your default JDK. To do that you need to follow the instructions "Configure Runtime for Projects" in the Visual Studio documentation:

    This requires you to edit setings. Type "Command+," (on a Mac) to open settings, in the settings field type:

    java.configuration.runtimes
    

    Click on the link displayed: "Edit in settings.json".

    The json file should be edited and saved to include this section (inside the outer { and } json structure):

    "java.configuration.runtimes": [
        {
           "name": "JavaSE-20",
           "path": "/Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home",
           "default": true
        }
    ]
    

    The name has to be one of JavaSE-NN where NN is a Java version. I use JavaSE-20, because that is the version of the JDK which I got from Zulu.

    The path is set to the path for the JDK that I got from the java_home command.

    OK, now you have your Java and IDE setup, you have to work on creating your project.

    Create a new folder on your machine (I used a terminal on a Mac to create a folder I named hellofx). Then on the Visual Studio "Welcome" page, choose "Open…" and choose the new folder you just created. It will prompt you if you trust the authors, choose "Yes, I trust the authors", if you trust yourself. In the Explorer on the left it will list your folder name in all caps, for me it says "HELLOFX". On the welcome screen choose "New File…", choose "New Java class". Copy and paste the HelloFX code from openjfx.io.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class HelloFX extends Application {
    
        @Override
        public void start(Stage stage) {
            String javaVersion = System.getProperty("java.version");
            String javafxVersion = System.getProperty("javafx.version");
            Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
            Scene scene = new Scene(new StackPane(l), 640, 480);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }
    

    Choose File | Save and enter the name "HelloFX.java".

    Click the arrow icon on the left of the screen and press "Run and Debug" to run the app.

    Your app will run and display a window using JavaFX with text in a Label, similar to that below:

    Hello, JavaFX 20.0.1, running on Java 20.0.1.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search