开发者

no such method error: ImmutableList.copyOf()

开发者 https://www.devze.com 2023-01-04 17:14 出处:网络
I\'m using Guava-05-snapshot, with Sun\'s JDK 1.6 The code blows up executing this snippet: List<String> badpasswords = Lists.newArrayList( Password.badWords);

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

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.

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

The Password.badWords is an ImmutableSet<String> and the creation of the writable array and the sort work perfectly. But attempts to 开发者_Go百科convert the Array into an ImmutableList fail.


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

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.

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.

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


This also works 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:

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


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:

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


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


Using Guava bundled with GWT worked.

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消