我正在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,都会自动创建其他版本。
我不知道为什么会发生这种事。
请使用以下依赖项。
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
</dependencies>
希望这能奏效。
类路径中很可能有两次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>