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
You better use the Java11 and above versions. And
vmArgs
is configured in launch.json, you need to use Run and Debug to debug, orStart Debugging
orRun Without Debugging
under the Run menu.JavaFX and Visual Studio Code
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:
That shows me:
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:
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):The name has to be one of
JavaSE-NN
whereNN
is a Java version. I useJavaSE-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.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: