-1

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

}
Maya
  • 19
  • 4
  • 3
    Have you tried following the instructions at https://openjfx.io/openjfx-docs/#install-javafx There is a vscode section with various options, including Maven. – James_D May 25 '22 at 22:41
  • 2
    See also [_Getting Started with JavaFX: JavaFX and Visual Studio Code_](https://openjfx.io/openjfx-docs/#IDE-VSCode). – trashgod May 25 '22 at 22:55
  • @James_D yes, that's the first step I took, didn't help :( – Maya May 25 '22 at 22:57
  • @trashgod yep, I've seen and followed the steps on that page too – Maya May 25 '22 at 22:58
  • Probably the best thing to do to get help would be to show your complete configuration here: post the `pom.xml` (if you want to use maven) file and the `launch.json` file,properly formatted as code, plus a simple sample application. Generally speaking, your error message means the module path is incorrect. – James_D May 25 '22 at 23:05
  • I recall going through the NetBeans portion of the guide more than once as my usage evolved. Also, try the example cited [here](https://stackoverflow.com/a/71288497/230513) to verify correct library installation. – trashgod May 25 '22 at 23:06
  • Sorry: correction. That error doesn’t indicate a problem with the module path, but with the imported libraries (as you identified). – James_D May 25 '22 at 23:17
  • Can you show your pom.xml and launch.json? By the way, can you successfully run the the example that trashgod mentioned? – MingJie-MSFT May 26 '22 at 05:22
  • @James_D I've added my launch.json and my example code. I'd prefer not to work with Maven if I can. – Maya May 26 '22 at 07:22
  • @MingJie-MSFT I've added the launch.json, and no, I can't. I get the same error that I get when trying to run my own example code, "java.lang.module.FindException: Module javafx.controls not found". – Maya May 26 '22 at 07:24
  • If you are struggling and having startup issues, I wouldn't spend time trying to solve it, just switch to Idea and use the [new JavaFX project wizard](https://www.jetbrains.com/help/idea/javafx.htm) and you will have a working project very quickly, just my opinion. – jewelsea May 26 '22 at 08:35

1 Answers1

0

"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

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • My naming system wasn't good, I apologize. The javafx-sdk-18.0.1 folder is the lib folder, the .jar files are directly inside of it. I've changed the location to somewhere less confusing and updated the main question, but unfortunately, the problem isn't from the folder I've specified. – Maya May 26 '22 at 08:06
  • @Maya I update my answer. I think it has nothing to do with the code or launch.json themself. If it still can't be solved, I suggest you start a new workspace and project. – MingJie-MSFT May 26 '22 at 09:38
  • thank you, this is exactly why I'm confused because every tutorial I've seen only includes the steps I've taken, and everyone claims to be able to run the projects with just that, but mine still doesn't work. You're probably right about there being some wrong settings of mine. However, I don't know where to look for settings that might affect this. – Maya May 26 '22 at 09:56