【问题标题】:newConcurrentHashSet() cannot be resolved from com.google.guava in IntelliJ?无法从 IntelliJ 中的 com.google.guava 解析 newConcurrentHashSet()?
【发布时间】:2020-03-30 07:08:42
【问题描述】:

我正在 macbook 上的 IntelliJ(社区 2019.2)中编译 java。

我的代码:

 import com.google.common.collect.Sets;

 public static void main(String[] args) {
    Sets.newConcurrentHashSet();// error ! cannot resolve the method !

}

我的 pom:

 <dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
    </dependency>

</dependencies>

基于

https://github.com/google/guava/blob/master/guava/src/com/google/common/collect/Sets.java

       Sets.newConcurrentHashSet();

应该可用。

但是,为什么我不能调用它?

更新 我跑了

 mvn dependency:tree

得到:

 +- com.google.guava:guava:jar:28.1-jre:compile
 [INFO] |  +- com.google.guava:failureaccess:jar:1.0.1:compile
 [INFO] |  +- com.google.guava:listenablefuture:jar:9999.0-empty-to- avoid-conflict-with-guava:compile

我没有看到与番石榴相关的其他依赖项。

当我检查时:

 ls -lrt ~/.m2/repository/com/google/guava/guava

我明白了:

28.1-jre
14.0.1
16.0.1
19.0
12.0.1
11.0.2
27.0-jre

虽然我删除了除 28.1-jre 之外的所有其他版本,但在我编译项目或打开 IntelliJ 时会自动创建其他版本。

我不知道为什么会发生这种情况。

【问题讨论】:

  • 这很奇怪。从 15.0 开始支持这种方法,我试过你的 pom 并且效果很好。

标签: java intellij-idea guava


【解决方案1】:

请使用下面的依赖。

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>28.1-jre</version>
    </dependency>

</dependencies>

希望这会奏效。

【讨论】:

    【解决方案2】:

    你的类路径中很可能有两次 Guava。

    使用以下 maven 命令检查您的依赖关系:

    mvn dependency:tree
    

    使用这个工具,找到你包含的包含旧的、不受支持的 Guava 版本的依赖项,并排除那个版本的 Guava(下面,我假设你发现它是工件 abc:xyz:1.2.3,所以相应地调整):

    <dependencies>
      <dependency>
        <groupId>abc</groupId><!-- The dependency which depends on the older version of Guava -->
        <artifactId>xyz</artifactId>
        <version>1.2.3</version>
        <!-- Add this below -->
        <exclusions>
          <exclusion>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
          </exclusion>
        </exclusions>
        <!-- Add this above -->
      </dependency>
      <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>28.1-jre</version><!-- or 28.1-android -->
      </dependency>
    </dependencies>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-09
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      相关资源
      最近更新 更多