Issue
I'm setting up a Maven project based on Java/JavaFX 11 which should be both editable in IntelliJ and Visual Studio Code.
My JDK is 11.0.3 (Windows x64) from https://adoptopenjdk.net/.
The pom.xml has a dependency for
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
My app can be compiled and run with "compiler:compile" and "exec:java" in IntelliJ. So step one succeeded.
Now when opening the same project in Visual Studio compiling and running does not succeed with a lot of similar error message
The package javafx.scene.chart is accessible from more than one module: <unnamed>, javafx.controls
I added a settings.json file in .vscode to point to the correct JDK directory.
{
"java.dependency.packagePresentation": "hierarchical",
"java.home": "C:/Program Files/Java/jdk-11.0.3+7",
"java.configuration.updateBuildConfiguration": "automatic"
}
But it still seems VSC has issues with two JavaFX versions, or isn't using the correct JDK. In the "Java Dependencies" view I can see references from both "JRE System Library" and "Maven" to JavaFX. Is there a way I can exclude them from the JRE view?
Screenshots as how JavaFX references are shown in VSC:
Solution
Based on the last screenshot posted, it seems that you are using a JDK 11 that does include JavaFX.
While most of JDK distributions don't include JavaFX (OpenJDK, Oracle, AdoptOpenJDK among others), some vendors are bundling JavaFX with the JDK, like Liberica.
If you check Liberica's release notes for JDK 11.0.2:
Windows x86_64, Mac, Linux x86_64 and Linux ARMv7 distributions contain OpenJFX 11.0.2.
While this option simplifies the use of JavaFX (no need to add it to the module-path, as it is already part of it), it can be confusing if you use Maven or Gradle build tools and include the JavaFX dependencies, or if you have other JDKs installed without it.
So the fix is easy: either use JDK without JavaFX and provide it via Maven/Gradle dependencies, or use JDK with JavaFX and don't include them.
I guess the latter is the preferred option, in case you need to export/share your project, since others might not have the same setup. Also it makes it more flexible to new releases of JavaFX.
As an aside, note that you can produce your own JDK that bundles JavaFX, following the instructions at https://openjfx.io/openjfx-docs/#modular, section Custom JDK+JavaFX image.
Answered By - José Pereda
Answer Checked By - Timothy Miller (JavaFixing Admin)