使用 com.google.common.base.Splitter 时出现 NoSuchMethodError 异常

发布于 2024-12-17 20:05:59 字数 783 浏览 2 评论 0原文

我尝试按如下方式使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流年已逝 2024-12-24 20:06:00

我遇到了同样的问题。
原来我用的是旧版本的番石榴。
访问此网站: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.

流云如水 2024-12-24 20:06:00

使用以下依赖项解决问题

要使用 Maven 添加对 Guava 的依赖项,请使用以下内容:

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

要使用 Gradle 添加依赖项:

dependencies {
  compile 'com.google.guava:guava:19.0'
}

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'
}
秋心╮凉 2024-12-24 20:06:00

您的问题是另一个库可能也包含番石榴库,并且首先从您的类路径而不是您想要的版本加载它。这将导致此运行时异常。

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.

甜心小果奶 2024-12-24 20:06:00

发生这种情况的另一个原因是 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.

青芜 2024-12-24 20:06:00

对我来说,当您有依赖于早期版本的 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

老街孤人 2024-12-24 20:06:00

有 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

找回味觉 2024-12-24 20:06:00

最近遇到这个问题了有用的是覆盖所有依赖项所使用的 Guava 版本:

implementation ("com.google.guava:guava") {
    version{
        strictly '31.1-android'
    }
}

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'
    }
}
梦里南柯 2024-12-24 20:06:00

是的,这只是番石榴库的问题。保留更新的库并删除番石榴的所有剩余版本(如果有)并尝试。应该可以正常工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文