Summary of issue/goal:
I wish to make a JavaFX GUI with Scene Builder in VSCode. A simple ‘hello world’ program runs in VSCode without issue. I can additionally run a simple ‘hello world’ JavaFX code in VSCode without issue. However, the JavaFX with Scene Builder code gives me an error.
This may be part of the issue, but ‘Run Code’ only works for non-JavaFX ‘hello world’. The only way to run JavaFX (non-Scene Builder) is via ‘Run Java’.
Versions:
- VSCode is up to date (v1.76)
- JavaFX was downloaded in past week or so (v19.0.2.1)
- Scene Builder was installed in past week or so (19.0.0 probably)
Related Extensions (no links; post marked spam and trying to get around it):
- Extension Pack for Java
- JavaFX Support
- Maven for Java (not sure if related)
- Project Manager for Java (not sure if related)
- SceneBuilder extension for Visual Studio Code
In detail (sorry, this will be a lot of code; wanted to give as much information possible):
Simple Hello World:
public class testing
{
public static void main(String [] args)
{
System.out.println("HI");
}
}
This code runs in ‘Run Code’ and ‘Run Java’ with output being HI
.
JavaFX Hello World:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
/**
* @param args
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
Using ‘Run Code’ gives 20 errors, here are two (my question was marked spam, maybe this changes it…?):
HelloWorld.java:1: error: package javafx.application does not exist
import javafx.application.Application;
^
HelloWorld.java:2: error: package javafx.event does not exist
import javafx.event.ActionEvent;
20 errors
Using ‘Run Java’ gives the expected output: a window with a button saying "Say ‘Hello World’" that prints "Hello World!" to the console upon click. Image: image here
More complicated Scene Builder
Code comes from this Youtube Video.
Here is the .fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="417.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainSceneController">
<children>
<Label layoutX="14.0" layoutY="14.0" prefHeight="19.0" prefWidth="392.0" text="Title" />
<TextField fx:id="tfTitle" layoutX="14.0" layoutY="33.0" prefHeight="27.0" prefWidth="392.0" />
<Button layoutX="184.0" layoutY="359.0" mnemonicParsing="false" onAction="#btnOkClicked" text="OK" />
</children>
</AnchorPane>
Here is the launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "testing",
"request": "launch",
"mainClass": "testing",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "TestingBasic",
"request": "launch",
"mainClass": "TestingBasic",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "App",
"request": "launch",
"mainClass": "App",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "HelloWorld",
"request": "launch",
"vmArgs": "--module-path C:/openjfx-19.0.2.1_windows-x64_bin-sdk/javafx-sdk-19.0.2.1/lib --add-modules javafx.controls,javafx.fxml",
"mainClass": "HelloWorld",
"projectName": "Java VSCode test_a97e49e3"
}
]
}
Here is MainSceneController.fxml:
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class MainSceneController {
@FXML
private TextField tfTitle;
@FXML
void btnOkClicked(ActionEvent event) {
Stage mainWindow = (Stage) tfTitle.getScene().getWindow();
String title = tfTitle.getText();
mainWindow.setTitle(title);
}
}
Here is the App.java:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("MainScene.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
}
}
public static void main(String[] args) {
System.out.println("got here");
launch(args);
}
}
No code has errors. Running main
in App.java
produces
Error: JavaFX runtime components are missing, and are required to run this application
.
(‘Run Java’ for App.java gives same thing; ‘Run Code’ for App.java gives 14 errors similar to the 20 earlier about java.fx packages not existing.)
I am confused what this means.
For reference, here is the ‘Configure runtime for projects’ page:
here. I have two options (17 and 19), but both fail and give me the runtime error: here.
If you could point me in the correct direction, that’d be great. I am new to StackOverflow so I appreciate the support and apologize if I’ve made a mistake.
2
Answers
Thanks to @Slaw for pointing me in the right direction.
Solution: It is a silly mistake. From my starting configuration I just had to:
For src folder: not sure why I need it, but taking them out caused problems.
For vmArgs: the section of form
"vmArgs": "--module-path "C:/Users/Oliver/Downloads/javafx-sdk-19.0.2.1/lib" --add-modules javafx.controls,javafx.fxml",
inlaunch.json
must be used for each 'type' that uses JavaFX.For example, here is my
launch.json
:to run all the java files in there. For people of my skill level note that: the vmArgs portion is in each and every* section; each section applies to each file (or java class) you are running.
*Technically not
I guess something wrong happens when you launch your JavaFX application.
For the first question
Run Code
andRun Java
are contributed from different extensions actually. The former one is contributed by Code Runner while the latter one from Java Debugger extension. I recommend to always useRun Java
because the Code runner cannot understand your project’s dependencies, you will get errors when your file uses third party libraries.For the second question
Can you try to launch your project directly from the launch panel and make sure the last configuration
HelloWorld
is selected?