无法将 Gson 导入我的 JavaFX Maven 项目

问题描述 投票:0回答:1

我正在 VSCode 中开发一个 JavaFX Maven 项目,并尝试将 Gson 导入到我的一个类中,但它不允许我这样做。 VSCode 给我一个“com.google.gson.Gson 类型不可访问”错误,并拒绝让我使用 Gson。

依赖项已添加到我的 pom.xml 文件中,我在网上尝试了一些解决方案,但没有一个起作用。同样奇怪的是 Gson 与我同时处理的另一个 Spring Maven 项目和这个 JavaFX 项目一起工作得很好,所以我不太确定问题可能是什么。

这是我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ca.cmpt213.asn5</groupId>
    <artifactId>client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <!-- <id>default-cli</id>
                        <configuration>
                            <mainClass>ca.cmpt213.asn5.</mainClass>
                        </configuration> -->
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这是我正在开发的无法导入 Gson 的类:

package ca.cmpt213.asn5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.Gson; //gives a squiggly red line under this statement

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SuperhumanTracker extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

        Label title = new Label("Superhuman Tracker");
        title.setFont(new Font(20));

        TableView table = new TableView<>();
        TableColumn idColumn = new TableColumn<>("ID");
        TableColumn nameColumn = new TableColumn<>("NAME");
        TableColumn superpowerColumn = new TableColumn<>("SUPERPOWER");
        TableColumn abilityScoreColumn = new TableColumn<>("ABILITY SCORE");
        idColumn.setMinWidth(25);
        nameColumn.setMinWidth(200);
        superpowerColumn.setMinWidth(150);
        abilityScoreColumn.setMinWidth(150);

        table.getColumns().addAll(idColumn, nameColumn, superpowerColumn, abilityScoreColumn);

        Button addButton = new Button("Add a Superhuman");

        ComboBox<Integer> idComboBox = new ComboBox<>();
        idComboBox.setPromptText("ID");
        idComboBox.getItems().addAll(1, 2, 3);

        Button viewButton = new Button("Get Details");
        Button deleteButton = new Button("Delete Superhuman");

        viewButton.setOnAction(event -> {
            try {
                long id = Long.valueOf(idComboBox.getValue());
                URL url = new URL("http://localhost:8080/api/superhuman/" + id);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = reader.readLine();

                Gson gson = new Gson(); //gives a squiggly red line under the Gson class

                System.out.println(connection.getResponseCode());
                connection.disconnect();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

            Scene scene = new Scene(new SuperhumanDetails(null, 0, 0, null, null, null, null, 0).getView(), 400, 500);
            Stage stage = new Stage();
            stage.setTitle("Superhuman Detailed View");
            stage.setScene(scene);
            stage.show();
        });

        HBox hboxLeft = new HBox(idComboBox, separator1, viewButton, separator2, deleteButton);
        hboxLeft.setAlignment(Pos.CENTER_LEFT);

        HBox hboxRight = new HBox(addButton);
        hboxRight.setAlignment(Pos.CENTER_RIGHT);

        HBox hboxControls = new HBox(hboxLeft, hboxRight);
        hboxControls.setSpacing(180);

        VBox vbox = new VBox(title, table, hboxControls);
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(10);
        vbox.setPadding(new Insets(10,10,10,10));

        Scene scene = new Scene(vbox, 600, 400);
        
        primaryStage.setTitle("Superhuman Tracker");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

这是 module-info.java 文件:

module ca.cmpt213.asn5 {
    requires transitive javafx.controls;

    exports ca.cmpt213.asn5;
}
java maven javafx gson
1个回答
0
投票

对于模块化应用程序,使用

module-info.java
,您需要提供适当的设置,以使 gson 模块可用于您的代码,并且您的代码可用于 gson。

这在 gson 故障排除文档的以下部分中有详细介绍:

解决方案:确保你的项目的 module-info.java 文件允许 Gson 在你的类上使用反射,例如:

module mymodule {
    requires com.google.gson;

    opens mypackage to com.google.gson;
}

或者,您可以通过删除

module-info.java
文件并按照 openjfx.io 上的 非模块化应用程序说明 来使应用程序成为非模块化应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.