Splitter blows up on simple Pattern
Asked Answered
M

3

9

I am just starting to us Guava in place of Google-Collections. The Splitter class seemed cool. But when I use it, like this:

private static final Pattern p = Pattern.compile(" +");
private static final Splitter usSplitter = Splitter.on(p).trimResults();

I get a stack dump:

java.lang.NoSuchMethodError: com.google.common.base.Platform.precomputeCharMatcher(Lcom/google/common/base/CharMatcher;)Lcom/google/common/base/CharMatcher;
        at com.google.common.base.CharMatcher.precomputed(CharMatcher.java:662)
        at com.google.common.base.CharMatcher.<clinit>(CharMatcher.java:69)
        at com.google.common.base.Splitter.<init>(Splitter.java:99)
        at com.google.common.base.Splitter.on(Splitter.java:208)

The javadocs have nothing about this "com.google.common.base.Platform." so its a bit hard to guess what is going wrong.

As you can see, the Pattern is dead simple.

Mannequin answered 27/6, 2010 at 3:19 Comment(0)
T
9

What version of Guava are you using? This works perfectly fine for me with r05.

Edit: It seems like the specific issue here is that you have both google-collections and guava in your runtime classpath. Platform (an internal class) existed in google-collections but didn't have the precomputedCharMatcher method. Splitter is being loaded from the guava jar properly, but Platform is being loaded from the google-collect jar.

Tremendous answered 27/6, 2010 at 3:56 Comment(5)
sorry, should have said: guava-r05-SNAPSHOT.jar Sun JDK 1.6 on Ubuntu 10.04 So I'm using R05 too, but its not working at all.Mannequin
There's a non-snapshot r05 on the Guava downloads page and available in Maven as well... try that.Tremendous
I've never even heard of a "guava-r05-SNAPSHOT".Tuition
Its the name of the jar file inside the current r05 download. I didn't name it, someone who built guava did.Mannequin
I just downloaded guava-r05.zip from the downloads page and the jar in it is guava-r05.jar.Tremendous
L
15

The java.lang.NoSuchMethodError tells you that the desired method is missing in the current runtime classpath while it was there in the compile time classpath.

In other words, to fix this problem you need to align your runtime classpath to have the correct version of the API as you used during compile time. It can also be caused by having different versions of the library mixed throughout the runtime classpath. Cleanup the classpath then.

Laudable answered 27/6, 2010 at 4:49 Comment(1)
Of course, detection of this problem is slightly complicated by the fact that the earliest versions of guava-.jar were called google-collect-.jar!Tuition
T
9

What version of Guava are you using? This works perfectly fine for me with r05.

Edit: It seems like the specific issue here is that you have both google-collections and guava in your runtime classpath. Platform (an internal class) existed in google-collections but didn't have the precomputedCharMatcher method. Splitter is being loaded from the guava jar properly, but Platform is being loaded from the google-collect jar.

Tremendous answered 27/6, 2010 at 3:56 Comment(5)
sorry, should have said: guava-r05-SNAPSHOT.jar Sun JDK 1.6 on Ubuntu 10.04 So I'm using R05 too, but its not working at all.Mannequin
There's a non-snapshot r05 on the Guava downloads page and available in Maven as well... try that.Tremendous
I've never even heard of a "guava-r05-SNAPSHOT".Tuition
Its the name of the jar file inside the current r05 download. I didn't name it, someone who built guava did.Mannequin
I just downloaded guava-r05.zip from the downloads page and the jar in it is guava-r05.jar.Tremendous
K
3

Actually. you got two version of com.google.common.base.Platform class and one of these classes has or hasn't the method.

Try to remove one of the jar files. I suggest to remove google-collections.jar and leave gauva.jar.

It will work fine.

Kindhearted answered 30/9, 2012 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.