gpt4 book ai didi

java - 为什么我在 maven 中添加的依赖项会出现模块丢失错误,并且我需要解决它?

转载 作者:行者123 更新时间:2023-12-02 10:14:11 24 4
gpt4 key购买 nike

错误:(10, 18) java: 包 com.google.gson 不可见
(com.google.gson 包在 gson 模块中声明,但 AnimalShelter 模块无法读取它)

在将 gson 依赖项添加到我的 Maven 后,我收到此错误,我完全不知道如何修复它,我用谷歌搜索了一下,建议我添加一个必需的在我的module-info.java中。这样做后,它修复了 IDE 错误,但是当我尝试运行时

java.lang.module.FindException:未找到模块 gson,AnimalShelter 需要

老实说,我根本不了解模块的工作原理,所以我可能会忽略一些简单的事情,但是在我的 maven pom.xml 中添加依赖项后它不应该工作吗? pom.xml 片段如下所示

pom.xml:(导入)

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>AnimalShelterYori</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/../mods</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
<dependencies>



<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>


<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>


<!-- ... -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
</project>

最佳答案

您可以尝试以下选项:

  1. Gson 依赖项的范围已提供,因此请确保在运行时这将由任何容器提供
  2. 更新 Maven 项目并进行全新安装
  3. 尝试使用最新版本的 Gson

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

关于java - 为什么我在 maven 中添加的依赖项会出现模块丢失错误,并且我需要解决它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54789354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com