Trouble importing external libraries into a Visual Studio project

694 views Asked by At

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 , 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 .

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");
    }

}
1

There are 1 answers

3
MingJie-MSFT On

"vmArgs": "--module-path /Users/mahya/Documents/University/Spring 2022/Advanced Programming/Assignments/A3/lib/javafx-sdk-18.0.1 --add-modules javafx.controls,javafx.fxml",

You should be accurate to the lib directory.

Adding /lib after javafx-sdk-18.0.1.

For example, this is my "vmArgs":

"vmArgs": "--module-path C:/Users/mingjiez/Downloads/openjfx-18.0.1_windows-x64_bin-sdk/javafx-sdk-18.0.1/lib --add-modules javafx.controls,javafx.fxml",

Supplementary notes:

I copied your code and launch.json(Part has been modified according to my settings).

I can run the code without any problem.

launch.json

        {
        "type": "java",
        "name": "Launch test",
        "request": "launch",
        "mainClass": "hellofx.test",
        "projectName": "HelloFx_6aa3362c",
        "vmArgs": "--module-path C:/Users/mingjiez/Downloads/openjfx-18.0.1_windows-x64_bin-sdk/javafx-sdk-18.0.1/lib --add-modules javafx.controls,javafx.fxml"
    },

result:

enter image description here

I think there must be something setting wrong while you didn't mention.

You can cheke the dependency in Java Project.

To add JavaFX as dependencies to your project, you can simply copy all the jar files from the lib folder of your downloaded JavaFX SDK, for instance /Users/your-user/Downloads/javafx-sdk-17.0.1/lib/ to the lib folder of your project. Or alternatively, you can add them via the Java Projects explorer. Click the + button beside the Referenced Library\ies, select the JavaFX library jars to add them.

enter image description here