Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot import Gson into my JavaFX Maven project

I am working on a JavaFX Maven project in VSCode and am trying to import Gson into one of my classes and it’s not letting me. VSCode gives me a "The type com.google.gson.Gson is not accessible" error and refuses to let me work with Gson.

The dependency has been added into my pom.xml file and I have tried some solutions online with none of them working. What’s also weird is Gson working fine with another Spring Maven project I’m working on at the same time as this JavaFX project, so I’m not quite sure what the problem could be.

Here’s my pom.xml file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

And here’s the class I’m working on that can’t import 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();
    }
}

Here’s the module-info.java file:

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

    exports ca.cmpt213.asn5;
}

>Solution :

For a modular application, with module-info.java you need to provide the appropriate settings to make the gson module available to your code and your code available to gson.

This is detailed in the gson trouble shooting documentation, under the section:

Make sure the module-info.java file of your project allows Gson to use reflection on your classes, for example:

module mymodule {
    requires com.google.gson;

    opens mypackage to com.google.gson;
}

Alternately, you can make your application non-modular by removing the module-info.java file and following the instructions for non-modular applications on openjfx.io.


Additionally, as noted in the comments, only use provided scope when you are sure that the provided dependency will be present in your runtime environment. Gson is not part of the standard JRE, so it is unlikely to be present.

From the Maven documentation:

provided

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. A dependency with this scope is added to the classpath used for compilation and test, but not the runtime classpath. It is not transitive.

Probably, your dependency should just use the default compile scope and read:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading