简体   繁体   English

将外部库导入 Visual Studio 项目时遇到问题

[英]Trouble importing external libraries into a Visual Studio project

I've been searching for how to set up JavaFX on my MacBook for three days and VSCode still tells me "import JavaFX couldn't be resolved."我一直在寻找如何在我的 MacBook 上设置 JavaFX 三天,VSCode 仍然告诉我“无法解决导入 JavaFX”。

I've tried setting up my project twice, once with Maven and once without.我已经尝试设置我的项目两次,一次使用 Maven,一次没有。 I downloaded the required JavaFX libraries through Maven and added the required dependencies to my pom.xml , but it didn't work.我通过 Maven 下载了所需的 JavaFX 库并将所需的依赖项添加到我的pom.xml中,但它不起作用。 I don't know much about Maven and there weren't a lot of instructions online, so I decided to ditch it.我对Maven了解不多,网上也没有很多说明,所以我决定放弃它。 I then downloaded the JavaFX SDK directly, added the .jar files to the "referenced libraries" of my project, added the vrm-arg line in launch.json , and tried cleaning my Java workspace through VSCode;然后我直接下载了 JavaFX SDK,将 .jar 文件添加到我项目的“引用库”中,在 launch.json 中添加了launch.json vrm-arg行,并尝试通过 VSCode 清理我的 Java 工作区; and yet still VSCode can't find the libraries.但是 VSCode 仍然找不到库。 I faced the same problem with setting up , so if anyone could instruct me on the whole "adding new libraries to your project" I'd be beyond grateful.我在设置时遇到了同样的问题,所以如果有人能指导我整个“向您的项目添加新库”,我将不胜感激。 I just can't find what step I'm missing.我只是找不到我缺少的步骤。

By the way, I am using Java 17. I also tried downloading Java 8 so that I wouldn't have to set up JavaFX separately, but the "download JDK" tab of VSCode wouldn't work, and downloading from the website seemed like just as much fuss.顺便说一句,我使用的是 Java 17。我也尝试下载 Java 8,这样我就不必单独设置 JavaFX,但是 VSCode 的“下载 JDK”选项卡不起作用,从网站下载似乎一样大惊小怪。 Also, that loophole wouldn't help me with .另外,那个漏洞对我没有帮助

Here's my launch.json :这是我的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",
            "name": "Launch App",
            "request": "launch",
            "vmArgs": "--module-path /Users/mahya/Lib/javafx-sdk-18.0.1/lib --add-modules javafx.controls,javafx.fxml",
            "mainClass": "App",
            "projectName": "A3_bdb16e50"
        }
    ]
}

And here's the example code I've been working with (VSCode automatically created it when I made a JavaFX project with Maven):这是我一直在使用的示例代码(当我使用 Maven 制作 JavaFX 项目时,VSCode 自动创建了它):

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class App extends Application {

    @Override
    public void start(Stage stage) {
        var javaVersion = SystemInfo.javaVersion();
        var javafxVersion = SystemInfo.javafxVersion();

        var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        var scene = new Scene(new StackPane(label), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

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

    public static String javaVersion() {
        return System.getProperty("java.version");
    }

    public static String javafxVersion() {
        return System.getProperty("javafx.version");
    }

}

"vmArgs": "--module-path /Users/mahya/Documents/University/Spring 2022/Advanced Programming/Assignments/A3/lib/javafx-sdk-18.0.1 --add-modules javafx.controls,javafx.fxml", "vmArgs": "--module-path /Users/mahya/Documents/University/Spring 2022/Advanced Programming/Assignments/A3/lib/javafx-sdk-18.0.1 --add-modules javafx.controls,javafx.fxml ",

You should be accurate to the lib directory.您应该准确到 lib 目录。

Adding /lib after javafx-sdk-18.0.1 .javafx-sdk-18.0.1之后添加/lib

For example, this is my "vmArgs":例如,这是我的“vmArgs”:

"vmArgs": "--module-path C:/Users/mingjiez/Downloads/openjfx-18.0.1_windows-x64_bin-sdk/javafx-sdk-18.0.1/lib --add-modules javafx.controls,javafx.fxml",

Supplementary notes:补充说明:

I copied your code and launch.json(Part has been modified according to my settings).我复制了您的代码和launch.json(部分已根据我的设置进行了修改)。

I can run the code without any problem.我可以毫无问题地运行代码。

launch.json启动.json

        {
        "type": "java",
        "name": "Launch test",
        "request": "launch",
        "mainClass": "hellofx.test",
        "projectName": "HelloFx_6aa3362c",
        "vmArgs": "--module-path C:/Users/mingjiez/Downloads/openjfx-18.0.1_windows-x64_bin-sdk/javafx-sdk-18.0.1/lib --add-modules javafx.controls,javafx.fxml"
    },

result:结果:

在此处输入图像描述

I think there must be something setting wrong while you didn't mention.我认为您没有提及时一定有什么设置错误。

You can cheke the dependency in Java Project.您可以在 Java Project 中检查依赖项。

To add JavaFX as dependencies to your project, you can simply copy all the jar files from the lib folder of your downloaded JavaFX SDK, for instance /Users/your-user/Downloads/javafx-sdk-17.0.1/lib/ to the lib folder of your project.要将 JavaFX 作为依赖项添加到您的项目中,您只需将所有 jar 文件从下载的 JavaFX SDK 的 lib 文件夹中复制,例如 /Users/your-user/Downloads/javafx-sdk-17.0.1/lib/ 到项目的 lib 文件夹。 Or alternatively, you can add them via the Java Projects explorer.或者,您可以通过 Java 项目资源管理器添加它们。 Click the + button beside the Referenced Library\ies, select the JavaFX library jars to add them.单击 Referenced Library\ies 旁边的 + 按钮,选择 JavaFX 库 jar 以添加它们。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM