带有 VSCode 的 JavaFX-11 [英] JavaFX-11 with VSCode

查看:57
本文介绍了带有 VSCode 的 JavaFX-11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定在这里遗漏了一些明显的东西......我正在试验 VSCode(来自 Eclipse),但我无法让 VSCode 看到 JavaFX11 库.在导入语句中,所有对 JavaFX 组件的引用都被标记:

[Java] 无法解析导入javafx

在 Eclipse 中,这是通过用户库"处理的,它会在 .classpath 中生成一个条目

<属性><属性名称=模块"值=真"/></属性></classpathentry>

虽然 VSCode 似乎可以理解 Eclipse 中 .classpath 的其余部分,但它并不理解这一点.用磁盘上的实际位置替换路径属性也不起作用.

为了清楚起见:

  • 这个问题专门针对 Java 11.在早期的 Java 版本中,JavaFX 是 JDK 的一部分.在 Java 11 中,它已移至一组外部模块.
  • 我不想使用 Maven 或 Gradle.我需要在不使用构建工具的情况下直接引用模块.

为了加分,如果 VSCode 也可以直接执行 JavaFX 应用程序就好了.但是,如果我必须使用显式模块路径和类路径从命令行启动应用程序,这是可以接受的

解决方案

我将运行来自

这显然意味着没有解析 JavaFX 类,即使 .classpath 文件中有条目:

无法解析此库.

解决JavaFX SDK

所以我要用我本地

虽然错误似乎已解决,但项目仍然无法构建.

解决 JRE

我们需要为项目设置 JDK 11,因此请从

修改后会弹出Java Language Server configuration changed, please restart VS Code.,重启一下.

尝试一下

我们可以看到没有任何错误,甚至还有一个 bin 文件夹,其中包含 VSCode 自动执行的构建结果.

我们可以运行它吗?如果我们尝试一下,我们会得到一个错误:

错误:缺少 JavaFX 运行时组件,需要运行此应用程序

这是在不指定模块路径的情况下使用 JavaFX 11 时出现的错误.

解决虚拟机参数

最后一步是添加所需的 vm 参数.

这可以在 launch.json 文件中完成.它包含一个默认配置,我们可以修改为 vmArgs 添加一个新条目,包括 --module-path 以及 JavaFX SDK 和 的本地路径--add-modules 带有所需的 JavaFX 模块:

<代码>{配置":[{"类型": "java","name": "CodeLens (Launch) - Main",请求":启动","vmArgs": "--module-path/Users//Downloads/javafx-sdk-11.0.2/lib--add-modules javafx.controls,javafx.fxml","mainClass": "hellofx.Main",项目名称":hellofx"}]}

现在我们已经准备好了一切.

再次运行项目,它应该可以工作了.

请注意,我是 VSCode 的第一次用户,所以我可能遗漏了一些明显的内容,也许其中一些步骤可以避免或简化.

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

解决方案

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

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.

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

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

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.

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.

这篇关于带有 VSCode 的 JavaFX-11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆