JavaFX-11 with VSCode
Asked Answered
S

3

22

I must be missing something obvious here... I am experimenting with VSCode (coming from Eclipse), but I am unable to get VSCode to see the JavaFX11 libraries. In the import statements, all references to JavaFX components are marked:

[Java] The import javafx cannot be resolved

In Eclipse, this is handled with a "User Library", which generates an entry in .classpath

<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX11">
    <attributes>
        <attribute name="module" value="true"/>
    </attributes>
</classpathentry>

While VSCode seemingly understands the rest of the .classpath from Eclipse, it does not understand this. Replacing the path attribute with the actual location on disk also does not work.

For clarity:

  • This question is specifically about Java 11. In earlier Java versions, JavaFX was part of the JDK. In Java 11, it has been moved to a set of external modules.
  • I do not want to use Maven or Gradle. I need to directly reference the modules without using a build tool.

For extra points, it would be nice if VSCode could also directly execute the JavaFX application. However, it is acceptable if I have to start the application from the command line with explicit module- and class-paths

Squamation answered 24/1, 2019 at 15:18 Comment(2)
Did you have a look at the docs here? openjfx.io/openjfx-docsOaks
I don't see anything there specific to VSCode. Using JavaFX-11 is not the problem. The problem is getting it to work within VSCode.Squamation
S
42

I'm going to run the HelloFX sample for Eclipse from the OpenJFX samples.

After I open the sample with VSCode, I see the reported error: [Java] The import javafx cannot be resolved [268435846].

JavaFX Error

This obviously means that JavaFX classes are not resolved, and even if there is an entry in the .classpath file:

<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX11"/>

this library can't be resolved.

Solving JavaFX SDK

So I'm going to replace that variable with the actual jars from my local JavaFX SDK:

<classpathentry kind="lib" path="/Users/<user>/Downloads/javafx-sdk-11.0.2/lib/javafx.base.jar"/>
<classpathentry kind="lib" path="/Users/<user>/Downloads/javafx-sdk-11.0.2/lib/javafx.graphics.jar"/>
<classpathentry kind="lib" path="/Users/<user>/Downloads/javafx-sdk-11.0.2/lib/javafx.controls.jar"/>
<classpathentry kind="lib" path="/Users/<user>/Downloads/javafx-sdk-11.0.2/lib/javafx.fxml.jar"/>

After refreshing the project, I can see under JAVA DEPENDENCIES these jars.

JavaFX SDK

While the error seems solved, the project still fails to build.

Solving JRE

We need to set JDK 11 for the project, so download it from here. Then open Eclipse and add it to the installed JREs. I see under Java -> Installed JREs -> Execution Environments that the name for the 11 version is JavaSE-11.

The .classpath file from the helloFX project also contains a reference to the JRE:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/
    org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JDK11">
    <attributes>
        <attribute name="module" value="true"/>
    </attributes>
</classpathentry>

so I'm going to replace JDK11 with JavaSE-11, and refresh the project. I can see under JAVA DEPENDENCIES that there is a reference to JRE System Library [JavaSE-11].

Solving JAVA_HOME

We need to set the java.home in VSCode.

This can be done in the settings.json from `Preferences->Settings->Workspace Settings:

{
   "java.dependency.packagePresentation": "hierarchical",
   "java.home":"/Users/<user>/Downloads/jdk-11.0.2.jdk/Contents/Home"
}

JavaHome

After modifying it, you'll get a popup with the message Java Language Server configuration changed, please restart VS Code., so restart it.

Trying it out

We can see that there are no errors, there is even a bin folder with the result of the build that automatically VSCode does.

Can we run it? If we try it out, we'll get an error:

Error: JavaFX runtime components are missing, and are required to run this application

Error JavaFX missing

This is the error you get when using JavaFX 11 without specifying the module-path.

Solving VM arguments

The final step consist on adding the required vm arguments.

This can be done in the launch.json file. It contains a default configuration, that we can modify adding a new entry for the vmArgs including the --module-path with the local path to the JavaFX SDK and --add-modules with the required JavaFX modules:

{
    "configurations": [
        {
            "type": "java",
            "name": "CodeLens (Launch) - Main",
            "request": "launch",
            "vmArgs": "--module-path /Users/<user>/Downloads/javafx-sdk-11.0.2/lib
                --add-modules javafx.controls,javafx.fxml",
            "mainClass": "hellofx.Main",
            "projectName": "hellofx"
        }
    ]
}

Now we have everything set.

Run the project again and it should work.

Running JavaFX 11

Note that I'm a first time user of VSCode, so I may have missed something obvious, and maybe some of these steps could be avoided or simplified.

Schaper answered 24/1, 2019 at 18:18 Comment(8)
I download VSC and all the Java stuff you pointed to. It all worked out well. Had one simple problem with "vmArgs": "--module-path /Users/<user>/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml",. I had to remove the newline and put it all on one line and it worked. My first attempt at Java 11.Thickskinned
@Sedrick Yes, I know it is one line only, but I split it so reader could see the --add-modules part.Butterwort
Thanks for the tutorial!Thickskinned
@José Will you add that to the other docs? openjfx.io/openjfx-docsOaks
@Oaks The OpenJFX docs try to help people getting started with JavaFX 11 on their favorite IDE, and if there are more IDEs other than the three major Java IDEs, that could be done, of course if there is enough interest. Problem is to limit the extension of this... Could you file an issue here?Butterwort
Wow - what a nice and amazingly thorough answer! Interestingly, did not need to set java.home. However, if you want to set this, the settings.json file may not be in the indicated location. To access it reliably, open the command palette (Ctrl-Shft-P), and then select the command "Open Settings (JSON)". This opens a nice editor where one can access user, workspace and folder settings.Squamation
@BradRichards I was using the Workspace Settings editor for that, but it is a json file nonetheless. About java.home, indeed it is not needed if you have JAVA_HOME already pointing to Java 11.Butterwort
If your module path has spaces in it you need to provide escaped quotes for it.Tulipwood
N
1

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",
        "request": "launch",
        "vmArgs": "--module-path /Volumes/Data/kits/installations/javafx-sdk-15.0.1/lib --add-modules=javafx.controls,javafx.fxml,javafx.graphics",
        "mainClass": "application.Main",
        "name": "Launch Main",
        "projectName": "GooDay"
  }
  ]
}

add this to settings json

 "java.dependency.packagePresentation": "hierarchical",
 "java.home":"/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home"
Neighboring answered 21/2, 2021 at 1:13 Comment(0)
H
-2

I think I've found a simpler answer!

  1. Start the executable that installs your JDK e.g.: jdk-8u201-windows-x64
  2. Reinstall and choose to have the extra features including JavaFX installed locally.

Once the installer was done Visual Studio was able to see the files and access was no longer restricted.

If this doesn't work, I did two other things before while I was trouble-shooting (neither of these solved the problem, but one of them could've affected the result of reinstalling the JDK):

  1. I appended a slash to the end of path to the JDK e.g.: "C:\Program Files\Java\jdk1.8.0_201\"

    1. Before reinstalling the JDK I connected eclipse to it and added a non-restricted access rule
Hock answered 9/2, 2019 at 21:41 Comment(1)
the question is about fx11 specifically.Ishii

© 2022 - 2024 — McMap. All rights reserved.