Java 没有这样的方法错误:ImmutableList.copyOf()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3126330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:40:43  来源:igfitidea点击:

no such method error: ImmutableList.copyOf()

javaguava

提问by fishtoprecords

I'm using Guava-05-snapshot, with Sun's JDK 1.6 The code blows up executing this snippet:

我正在使用 Guava-05-snapshot 和 Sun 的 JDK 1.6 代码在执行这个片段时会崩溃:

List<String> badpasswords = Lists.newArrayList( Password.badWords);
Collections.sort(badpasswords);
ImmutableList<String> tmp = ImmutableList.copyOf(badpasswords);

Specifically on the ImmutableList.copyOf() call. This code has worked for months, using the old Google-Collections code.

特别是在 ImmutableList.copyOf() 调用上。这段代码使用旧的 Google-Collections 代码已经运行了几个月。

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;

The Password.badWordsis an ImmutableSet<String>and the creation of the writable array and the sort work perfectly. But attempts to convert the Array into an ImmutableListfail.

Password.badWords是一个ImmutableSet<String>与可写阵列和排序工作完美的创造。但是尝试将 Array 转换为ImmutableList失败。

采纳答案by Kevin Bourrillion

Guava is a fully compatible superset of Google Collections -- we did not change anything in an incompatible way. (This is tested by running the entire Google Collections test suite (which is extensive) against the lastest guava jar.)

Guava 是一个完全兼容的 Google Collections 超集——我们没有以不兼容的方式改变任何东西。(这是通过针对最新的番石榴 jar运行整个 Google Collections 测试套件(非常广泛)来测试的。)

I believe you have a copy of google-collect-*.jar still makings its way into your classpath. Either explicitly, or because some other jar included it without repackaging it. You just have to find it and remove it.

我相信你有一份 google-collect-*.jar 的副本仍在你的类路径中。要么是明确的,要么是因为其他一些 jar 包含它而没有重新包装它。你只需要找到它并删除它。

In Google Collections, there was an ImmutableList.copyOf(Iterable)method, and there was no public ImmutableList.copyOf(Collection)method. Which is fine, because a collection is also an iterable. In Guava, we've added the Collection overload. This is completely compatible, as all source that used to compile still can, and any source previously compiled will simply still reference the original method.

在 Google Collections 中,有一个ImmutableList.copyOf(Iterable)方法,而没有公共ImmutableList.copyOf(Collection)方法。这很好,因为集合也是可迭代的。在 Guava 中,我们添加了 Collection 重载。这是完全兼容的,因为用于编译的所有源代码仍然可以,并且以前编译的任何源代码仍将引用原始方法。

The problem comes in if you compile against Guava but then run against Google Collections. I believe that is likely what is happening.

如果您针对 Guava 进行编译,然后针对 Google Collections 运行,则会出现问题。我相信这很可能正在发生。

回答by ColinD

This alsoworks fine for me using the official (non-snapshot) guava-r05 release from Maven. Incidentally, this might be a little nicer way of doing the same thing:

使用 Maven 的官方(非快照)guava-r05 版本,这对我来说很好用。顺便说一句,这可能是做同样事情的更好的方式:

ImmutableList<String> sorted = Ordering.natural()
    .immutableSortedCopy(Password.badWords);

回答by eddyparkinson

Using Guava bundled with GWTworked.

使用与 GWT 捆绑的 Guava有效。

I added both Guava Jar files (Version 13) from here code.google.com/p/guava-libraries to my war/WEB-INF/lib and added guava-13.0.1.jar to my build path (right click & add to build path)

我将 code.google.com/p/guava-libraries 中的两个 Guava Jar 文件(版本 13)添加到我的 war/WEB-INF/lib 并将 guava-13.0.1.jar 添加到我的构建路径(右键单击并添加建立路径)

回答by noorani

1) download guava-XX.X.X.jar from http://code.google.com/p/guava-libraries/2) in eclipse right click on the project select build path and add this jar

1) 从http://code.google.com/p/guava-libraries/下载 guava-XX.XXjar 2) 在 eclipse 中右键单击项目选择构建路径并添加这个 jar

回答by anre

If the error occurs when deploying a web application to WebLogic 12c (but the guava JAR is in WEB-INF/lib), the following configuration in weblogic.xml will help to solve it:

如果在将web应用部署到WebLogic 12c时出现错误(但是guava JAR在WEB-INF/lib中),weblogic.xml中如下配置可以帮助解决:

<container-descriptor>
    <prefer-application-packages>
        <package-name>com.google</package-name>
    </prefer-application-packages>
</container-descriptor>