vscode启动项目报jdk11没有,带有VSCode的JavaFX-11

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

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

解决方案

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

ICLAp.png

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

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:

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

6vmpK.png

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:

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//Downloads/jdk-11.0.2.jdk/Contents/Home"

}

8Jant.png

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

TXSq0.png

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

uyfD4.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值