使用 com.google.common.base.Splitter 时出现 NoSuchMethodError 异常
我尝试按如下方式使用 com.google.common.base.Splitter
Iterable<String> segs = Splitter.on("/").split("one/two/three/four/five");
for (String seg : segs) {
System.out.println(seg);
}
但是,我看到以下异常:
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)
有人知道我在这里做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我遇到了同样的问题。
原来我用的是旧版本的番石榴。
访问此网站:https://code.google.com/p/guava-libraries/< /a>,然后下载更新版本。
顺便说一句,google-collections 已重命名为 Guava。
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.
要使用 Maven 添加对 Guava 的依赖项,请使用以下内容:
要使用 Gradle 添加依赖项:
To add a dependency on Guava using Maven, use the following:
To add a dependency using Gradle:
您的问题是另一个库可能也包含番石榴库,并且首先从您的类路径而不是您想要的版本加载它。这将导致此运行时异常。
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.
发生这种情况的另一个原因是 GSON 库在 Guava 库之前导入。
看:
https://github.com/google/guava/issues/2786
我正在导入将文件夹中的 jar 放入 IntelliJ 中。最终的工作是在 gson 旁边添加一个“z”(因此 jar 将被命名为 zgson),以便 Guava 首先导入。
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.
对我来说,当您有依赖于早期版本的 Guava 的依赖项时,就会发生这种情况,并且该依赖项首先列出。 Guava 将在最先发现的地方解决并忽略其余部分。
修复方法是首先添加对 guava 的依赖,但要注意不要破坏其他使用 guava 的项目
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
有 2 个版本:1) com.google.guava:guava:26.0-android 2) com.google.guava:guava:26.0-jre 。最有可能的是,您分配了错误的版本,就像我的情况一样
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
最近遇到这个问题了有用的是覆盖所有依赖项所使用的 Guava 版本:
Got that issue recently. What helps is to override the used version of Guava for all dependencies:
是的,这只是番石榴库的问题。保留更新的库并删除番石榴的所有剩余版本(如果有)并尝试。应该可以正常工作。
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.