33

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 convert the Array into an ImmutableList fail.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
fishtoprecords
  • 2,394
  • 7
  • 27
  • 38
  • The same problem is answered in your subsequent question: http://stackoverflow.com/questions/3126276/splitter-blows-up-on-simple-pattern/3126420#3126420 – BalusC Jun 27 '10 at 04:56
  • 2
    You should probably upvote/accept answers that are useful, by the way. – ColinD Jun 27 '10 at 19:35

5 Answers5

46

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.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
  • 2
    The problem was a side effect of Netbeans keeping too many things in caches. A "clean and build" did nothing to fix things. I did a complete clean and build down into every jar in the project. The solution was to exit Netbeans, go to ~/netbeans/6.7/var/cache and do a rm -rf * – fishtoprecords Jun 27 '10 at 23:47
  • 2
    "Guava is a fully compatible superset of Google Collections" - Apparently not anymore? http://code.google.com/p/gdata-java-client/issues/detail?id=344#c2 – Paul Bellora Mar 22 '12 at 23:18
  • Yes, some things have indeed hit their "deprecated for 18 months" end-of-life point, it's true. – Kevin Bourrillion May 21 '12 at 15:05
  • (That's only as of release 11.0, of December 2011.) – Kevin Bourrillion May 21 '12 at 15:08
  • 1
    I had a similar issue, but with com.google.common.collect.Iterables.getFirst. Thanks for the solution! – Ritesh Oct 17 '12 at 17:29
  • I got the similar error when the IDE tell me it cannot resolved google collection, the mvn build is successful. I did a mvn clean, and rebuild it, all works. – Allan Ruin May 29 '15 at 12:44
  • You can use the code snippet in this answer to pinpoint where the jar is coming from - http://stackoverflow.com/a/12044668/678860 – warunapww Jul 05 '16 at 09:14
  • Replacing guava collections with google collections solved the issue. – Abhi Muktheeswarar Jun 17 '17 at 14:31
4

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);
ColinD
  • 108,630
  • 30
  • 201
  • 202
2

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>
anre
  • 3,617
  • 26
  • 33
1

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

noorani
  • 11
  • 1
0

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)

Community
  • 1
  • 1
eddyparkinson
  • 3,680
  • 4
  • 26
  • 52