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

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

问题描述

我肯定在这里缺少明显的东西...我正在尝试VSCode(来自Eclipse),但是我无法获得VSCode来查看JavaFX11库.在import语句中,标记了对JavaFX组件的所有引用:

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

在Eclipse中,这由用户库"处理,该库在.classpath中生成一个条目

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>

尽管VSCode似乎可以从Eclipse理解.classpath的其余部分,但它不理解这一点.用磁盘上的实际位置替换path属性也不起作用.

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.

为清楚起见:

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

对于其他问题,如果VSCode也可以直接执行JavaFX应用程序,那将是很好的选择.但是,如果必须从命令行使用显式的模块路径和类路径启动应用程序,则可以接受

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

推荐答案

我将从使用VSCode打开示例后,看到报告的错误:[Java] The import javafx cannot be resolved [268435846].

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

这显然意味着即使在.classpath文件中有一个条目,JavaFX类也无法解析:

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

该库无法解析.

解决JavaFX SDK

因此,我将用本地 JavaFX SDK 中的实际jar替换该变量:

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

刷新项目后,可以在JAVA DEPENDENCIES下看到这些罐子.

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

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

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

解决JRE

我们需要为该项目设置JDK 11,因此可以从此处下载.然后打开Eclipse并将其添加到已安装的JRE中.我在Java -> Installed JREs -> Execution Environments下看到11版本的名称是JavaSE-11.

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.

helloFX项目中的.classpath文件还包含对JRE的引用:

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>

所以我要用JavaSE-11替换JDK11,并刷新项目.我可以在JAVA DEPENDENCIES下看到对JRE System Library [JavaSE-11]的引用.

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

解决JAVA_HOME

我们需要在VSCode中设置java.home.

We need to set the java.home in VSCode.

这可以在settings.json的首选项"->设置"->工作空间设置"中完成:

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

修改后,您会看到一个弹出窗口,显示消息Java Language Server configuration changed, please restart VS Code.,因此请重新启动它.

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

试用

我们可以看到没有错误,甚至还有一个bin文件夹,其结果是VSCode自动完成的.

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

这是在使用JavaFX 11时未指定模块路径的错误.

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

解决虚拟机参数

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

The final step consist on adding the required vm arguments.

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

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.

请注意,我是VSCode的首次用户,因此我可能错过了一些显而易见的事情,并且也许可以避免或简化其中的某些步骤.

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天全站免登陆