NoSuchMethodError exception when using com.google.common.base.Splitter
Asked Answered
O

8

24

I'm trying to use com.google.common.base.Splitter as follows

Iterable<String> segs = Splitter.on("/").split("one/two/three/four/five");

for (String seg : segs) {
  System.out.println(seg);
}

However, I'm seeing the following exception:

Exception in thread "main" 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:664)
    at com.google.common.base.CharMatcher.<clinit>(CharMatcher.java:71)
    at com.google.common.base.Splitter.<init>(Splitter.java:107)
    at com.google.common.base.Splitter.on(Splitter.java:171)
    at Test.main(Test.java:30)

Does anyone have any idea what I'm doing wrong here?

Official answered 24/11, 2011 at 13:4 Comment(10)
Do you have two versions of Guava (or its predecessor) in your classpath perhaps?Musicianship
No I don't think so. This is the first time I've downloaded and tried to use Guava.Official
You might have a google collection jar in your classpath. Some libraries depend on it.Hyperdulia
possible duplicate of Splitter blows up on simple PatternDriblet
Hmm. I really don't think this is my classpath. What the easiest was to check in eclipse? It's not on my build path.Official
Looks like another of the third party libraries which I was including is already using either Google Collections or Guava. Removed all third party libraries except for Guava and it worked. Thanks for the response.Official
This proved useful java-tips.org/java-se-tips/java.lang/…Official
If you use Maven, the dependency:tree plugin is pretty awesome to find these transitive dependencies: maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.htmlHyperdulia
@mip: Note also that if you're using Maven you can find what dependency was including Google Collections and add an exclusion to that dependency to prevent it from including it.Driblet
Such type of errors could occur also if you are using guava-jdk library together with guava.Sanches
R
10

I encountered the same problem. It turned out that I used a older version of guava. Go to this website:https://code.google.com/p/guava-libraries/, and download a newer version.

By the way,google-collections was renamed to Guava.

Rola answered 23/4, 2014 at 19:19 Comment(0)
A
9

Use the below dependency to fix the issue

To add a dependency on Guava using Maven, use the following:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>19.0</version>
</dependency>

To add a dependency using Gradle:

dependencies {
  compile 'com.google.guava:guava:19.0'
}
Artistry answered 7/9, 2016 at 6:44 Comment(0)
T
2

Your problem is that another library might also contain a guava library and it's being loaded first from your classpath instead of the version you want. This would cause this runtime exception.

Topo answered 17/6, 2013 at 18:16 Comment(0)
M
0

Another reason this happens is if the GSON library is imported before the Guava library.

See: https://github.com/google/guava/issues/2786

I was importing the jars from a folder into IntelliJ. What ended up working was add a "z" next to gson (so the jar would be named zgson) so that Guava would import first.

Musil answered 14/6, 2018 at 21:51 Comment(0)
A
0

For me this happens when you have a dependency which depends on an earlier version of Guava, and this dependency is listed first. Guava will be resolved at the place it was found first and ignore the rest.

The fix is to add the dependency on guava first, but careful that it does not break other projects which use guava

Applique answered 28/6, 2018 at 20:1 Comment(0)
A
0

There are 2 versions: 1) com.google.guava:guava:26.0-android 2) com.google.guava:guava:26.0-jre . Most likely, you assign wrong version as in my case

Actinoid answered 13/8, 2018 at 14:51 Comment(0)
S
0

Got that issue recently. What helps is to override the used version of Guava for all dependencies:

implementation ("com.google.guava:guava") {
    version{
        strictly '31.1-android'
    }
}
Sixfooter answered 27/9, 2022 at 13:17 Comment(0)
A
-2

Yeah, It's the problem with guava library only. Keep the updated library and remove all remaining versions of guava if you have any and try. Should work fine.

Aquatic answered 1/10, 2014 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.