代码之家  ›  专栏  ›  技术社区  ›  Gyro

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

  •  1
  • Gyro  · 技术社区  · 1 年前

    我正在VSCode中进行JavaFXMaven项目,并试图将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;
    }
    
    1 回复  |  直到 1 年前
        1
  •  2
  •   jewelsea    1 年前

    用于模块化应用

    对于模块化应用程序,具有 module-info.java 您需要提供适当的设置,使gson模块可用于您的代码,使您的代码可用于gson。

    Gson故障排除文档中的以下部分对此进行了详细说明:

    确保 fo.java模块 项目的文件允许Gson对类使用反射,例如:

    module mymodule {
        requires com.google.gson;
    
        opens mypackage to com.google.gson;
    }
    

    对于您的项目

    更改:

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

    至:

    module ca.cmpt213.asn5 {
        requires javafx.controls;
        requires com.google.gson;
    
        opens mypackage to com.google.gson;
    
        exports ca.cmpt213.asn5;
    }
    

    哪里 mypackage 是Gson需要在其上运行反射的代码中的某个包。

    虽然这不会引起任何问题,但我只是注意到 transitive 您的应用程序不需要关键字。它用于使依赖模块中的类型对模块的客户端可用。这是一个对于创建可共享库模块比对于独立的JavaFX应用程序更有用的函数。

    对于非模块化应用

    或者,您可以通过删除 fo.java模块 文件并遵循 instructions for non-modular applications on openjfx.io

    使用适当的maven范围

    如评论中所述,仅使用 provided 当您确信所提供的依赖项将出现在您的运行时环境中时,作用域。Gson不是标准JRE的一部分,因此它不太可能出现。

    来自 Maven documentation :

    假如

    这很像 compile ,但表示您希望JDK或容器在运行时提供依赖关系。例如,当为Java Enterprise Edition构建web应用程序时,您需要将对Servlet API和相关Java EE API的依赖设置为所提供的范围,因为web容器提供了这些类。具有此作用域的依赖项被添加到用于编译和测试的类路径中,但不添加到运行时类路径中。它是不可传递的。

    也许,您的依赖项应该只使用默认值 编写 范围和阅读:

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>