Home > Enterprise >  JavaFX stops working when module-info.java file created in VSCode
JavaFX stops working when module-info.java file created in VSCode

Time:10-18

When I add the module-info.java file to the src folder in my java project I get errors: Image of the errors and my folder setup

When I remove the module-info.java file the errors go, but I need the module-info file

module-info.java:

module JavaFXtest{

}

haven't started adding things, however whenever I add a requirement, it says eg: javafx.fxml cannot be resolved to a module (all the javafx jar files are in the referenced libraries) thanks in advanced

CodePudding user response:

Your module-info.java should specify which other modules you require, and if necessary should export packages from your module. See, e.g. https://www.oracle.com/corporate/features/understanding-java-9-modules.html for a description.

module JavaFXtest{
    requires javafx.controls ;
    requires javafx.fxml ;
    exports com.example.mypackage ;
}

where the last line specifies the package with your Application subclass.

You also need to ensure that the JavaFX modules are on the module path. The preferred approach is to use a dependency management tool, such as Maven or Gradle, to manage all this for you. See the "Getting Started" instructions at OpenJFX.

CodePudding user response:

To fix this, I edited the launch.json file and added this:

"vmArgs": "--module-path \ "/Users/(name)/Documents/lib/javafx-sdk- 17.0.0.1/lib\ " --add-modules javafx.controls,javafx.fxml"

where name is the users name. The directory leads to the directory of the library folder of the javafx-sdk.

the path must be in "", so I added \ before the " so this works and so it doesn't escape the outer " ".

What this does is when you run your application, vscode adds the arguments to add the required modules from the referenced libraries at runtime. Therefore you do not need a module-info.java file to run.

  • Related