在VScode中使用JavaFX。JavaFX运行时组件缺失。即使我有vmArgs

huangapple go评论65阅读模式
英文:

JavaFX in VScode. JavaFX runtime components are missing. Even though I have vmArgs

问题

我正在使用VScode学习JavaFX,但我不断收到错误消息,其中包含:“错误:缺少JavaFX运行时组件,需要运行此应用程序”,尽管我已在launch.json中输入了vmArgs并将JavaFX添加到了已引用库中,正如多个教程视频中所示。我的vmArgs看起来类似于这样:

"vmArgs": "--module-path C:/.../Desktop/javafx-sdk-20.0.1/lib --add-modules javafx.controls,javafx.fxml", 

我尝试过多个vmArgs,包括将JavaFX存储在下载文件夹中,但错误仍然存在。我怀疑可能与我的配置Java Runtime有关,它似乎为空,或与JAVA_HOME有关的某些问题。有人可以帮助我解决这个问题吗?提前感谢您。

英文:

I am learning JavaFX using VScode, but I am continuously getting an error message stating, "Error: JavaFX runtime components are missing and are required to run this application", despite having entered vmArgs in launch.json and adding JavaFX to the referenced library as shown in multiple tutorial videos. My vmArgs look something like this:

"vmArgs": "--module-path C:/.../Desktop/javafx-sdk-20.0.1/lib --add-modules javafx.controls,javafx.fxml", 

I have tried multiple vmArgs, including having the JavaFX stored in the Download folder, but the error persists. I suspect that there may be an issue with my Configure Java Runtime, which appears to be empty, or something related to JAVA_HOME. Can someone help me resolve this issue? Thank you in advance.

答案1

得分: 3

我所做的是删除了Java和一切内容,重新下载了一切,然后按照这个使用JavaFX入门:JavaFX和Visual Studio Code的指南进行操作。它起作用了。

编辑:有时候需要很长时间才能起作用,尽管我已经保存了我的launch.json和setting.json文件。只需复制整个有效的文件,这样可以节省很多时间。

英文:

What I did is I deleted Java and everything, downloaded everything all over again, and followed this Getting Started with JavaFX: JavaFX and Visual Studio Code.
It worked.

Edit: Sometimes it takes a long time to work, even though I have already saved my launch.json and setting.json files. Just make a copy of the whole file that works, it will save a lot of time.

答案2

得分: 2

标准解决方案是:

我建议(如果你真的想要使用Visual Studio),不要使用本答案中其余的内容。


另一种解决方案是使用包含 JavaFX 的 JDK。

这些说明对我有效。不能保证它们对你有效,或者在未来不经修改就能继续使用。我也不提供对这些说明的支持,它们是按原样呈现的。


Visual Studio Code 和 Azul Zulu "JDK FX" 分发,其中包括 JavaFX

下载并安装 Azul Zulu "JDK FX" 适用于你的操作系统和架构。

这是一个针对 Java 20、OS X、x64、JDK FX 包的示例下载链接,你可以访问链接并根据你的系统更改设置,然后下载并运行安装程序(我选择了 Mac 上的 .dmg 安装程序)。

这将在你的系统上安装 JDK 到某个位置。在 Mac 上,你可以通过输入以下命令来查找:

/usr/libexec/java_home -V

这会显示:

20.0.1 (x86_64) "Azul Systems, Inc." - "Zulu 20.30.11" /Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home

所以现在我知道 JDK 安装在哪里了。

然后在 Visual Studio 中设置 JDK,转到 Visual Studio 网站上的 "Java: Getting Started":

选择 "Install the Coding Pack for Java - macOS"(如果你使用的是 Windows,则选择 Windows 链接)。

一旦 VSCode 安装完成,将 Zulu 设置为默认的 JDK。为此,你需要按照 Visual Studio 文档中的 "Configure Runtime for Projects" 说明:

这需要你 编辑设置。在 Mac 上,按 "Command+," 打开设置,在设置字段中键入:

java.configuration.runtimes

单击显示的链接:"Edit in settings.json"。

应该编辑并保存 json 文件,以包括此部分(在外部 {} json 结构内):

"java.configuration.runtimes": [
    {
       "name": "JavaSE-20",
       "path": "/Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home",
       "default": true
    }
]

名称必须是 JavaSE-NN,其中 NN 是 Java 版本。我使用的是 JavaSE-20,因为这是我从 Zulu 获取的 JDK 版本。

路径设置为我从 java_home 命令获取的 JDK 路径。

现在你已经设置好了你的 Java 和 IDE,接下来你需要创建你的项目。

在你的机器上创建一个新文件夹(我在 Mac 上使用终端创建了一个名为 hellofx 的文件夹)。然后在 Visual Studio 的 "Welcome" 页面上,选择 "Open..." 并选择刚刚创建的新文件夹。它会提示你是否信任作者,如果你信任自己的话,选择 "Yes, I trust the authors"。在左侧的资源管理器中,它会列出文件夹名(对我来说是大写),比如 "HELLOFX"。在欢迎屏幕上选择 "New File...",然后选择 "New Java class"。复制并粘贴来自 openjfx.io 的 HelloFX 代码

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

选择文件 | 保存,并输入名称 "HelloFX.java"。

点击屏幕左侧的箭头图标,然后点击 "Run and Debug" 来运行应用程序。

你的应用程序将运行,并在 Label 中显示使用 JavaFX 的窗口,类似于以下内容:

Hello, JavaFX 20.0.1, running on Java 20.0.1.
英文:

The standard solution is:

I recommend that (if you really want to use Visual Studio), not what is in the rest of this answer.


An alternate solution is to use a JDK which includes JavaFX.

These instructions worked for me. I make no guarantee they will work for you or continue to work without modification in the future. I also do not offer support for these instructions, they are presented "as-is".


Visual Studio Code and Azul Zulu "JDK FX" distribution, which includes JavaFX

Download and install Azul Zulu "JDK FX" for your OS and architecture.

Here is a link to an example download for Java 20, OS X, x64, JDK FX package, you can go to the link and change the settings for your system, then download and run the installer (I choose a .dmg installer for Mac).

That will install the JDK "somewhere" on your system. On a Mac you can find out where by typing:

/usr/libexec/java_home -V

That shows me:

20.0.1 (x86_64) "Azul Systems, Inc." - "Zulu 20.30.11" /Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home

So now I know where the JDK is installed.

Then to setup the JDK in Visual Studio, go to "Java: Getting Started" on the Visual Studio site:

Choose "Install the Coding Pack for Java - macOS" (use the Windows link instead if that is your OS).

Once the VSCode installation is done, set up Zulu as your default JDK. To do that you need to follow the instructions "Configure Runtime for Projects" in the Visual Studio documentation:

This requires you to edit setings. Type "Command+," (on a Mac) to open settings, in the settings field type:

java.configuration.runtimes

Click on the link displayed: "Edit in settings.json".

The json file should be edited and saved to include this section (inside the outer { and } json structure):

"java.configuration.runtimes": [
    {
       "name": "JavaSE-20",
       "path": "/Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home",
       "default": true
    }
]

The name has to be one of JavaSE-NN where NN is a Java version. I use JavaSE-20, because that is the version of the JDK which I got from Zulu.

The path is set to the path for the JDK that I got from the java_home command.

OK, now you have your Java and IDE setup, you have to work on creating your project.

Create a new folder on your machine (I used a terminal on a Mac to create a folder I named hellofx). Then on the Visual Studio "Welcome" page, choose "Open..." and choose the new folder you just created. It will prompt you if you trust the authors, choose "Yes, I trust the authors", if you trust yourself. In the Explorer on the left it will list your folder name in all caps, for me it says "HELLOFX". On the welcome screen choose "New File...", choose "New Java class". Copy and paste the HelloFX code from openjfx.io.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

Choose File | Save and enter the name "HelloFX.java".

Click the arrow icon on the left of the screen and press "Run and Debug" to run the app.

Your app will run and display a window using JavaFX with text in a Label, similar to that below:

Hello, JavaFX 20.0.1, running on Java 20.0.1.

答案3

得分: 0

你最好使用Java11及以上版本。而vmArgs是在launch.json中配置的,你需要使用运行和调试来调试,或者在运行菜单下使用开始调试无调试运行

在VScode中使用JavaFX。JavaFX运行时组件缺失。即使我有vmArgs

JavaFX和Visual Studio Code

英文:

You better use the Java11 and above versions. And vmArgs is configured in launch.json, you need to use Run and Debug to debug, or Start Debugging or Run Without Debugging under the Run menu.

在VScode中使用JavaFX。JavaFX运行时组件缺失。即使我有vmArgs

JavaFX and Visual Studio Code

huangapple
  • 本文由 发表于 2023年6月16日 07:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定