I've been searching for how to set up JavaFX on my MacBook for three days and VSCode still tells me "import JavaFX couldn't be resolved."
I've tried setting up my project twice, once with Maven and once without. I downloaded the required JavaFX libraries through Maven and added the required dependencies to my pom.xml
, but it didn't work. I don't know much about Maven and there weren't a lot of instructions online, so I decided to ditch it. I then downloaded the JavaFX SDK directly, added the .jar files to the "referenced libraries" of my project, added the vrm-arg
line in launch.json
, and tried cleaning my Java workspace through VSCode; and yet still VSCode can't find the libraries. I faced the same problem with setting up json, so if anyone could instruct me on the whole "adding new libraries to your project" I'd be beyond grateful. I just can't find what step I'm missing.
By the way, I am using Java 17. I also tried downloading Java 8 so that I wouldn't have to set up JavaFX separately, but the "download JDK" tab of VSCode wouldn't work, and downloading from the website seemed like just as much fuss. Also, that loophole wouldn't help me with json.
Here's my 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": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch App",
"request": "launch",
"vmArgs": "--module-path /Users/mahya/Lib/javafx-sdk-18.0.1/lib --add-modules javafx.controls,javafx.fxml",
"mainClass": "App",
"projectName": "A3_bdb16e50"
}
]
}
And here's the example code I've been working with (VSCode automatically created it when I made a JavaFX project with Maven):
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class App extends Application {
@Override
public void start(Stage stage) {
var javaVersion = SystemInfo.javaVersion();
var javafxVersion = SystemInfo.javafxVersion();
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
class SystemInfo {
public static String javaVersion() {
return System.getProperty("java.version");
}
public static String javafxVersion() {
return System.getProperty("javafx.version");
}
}