使用Google Translate API获得java.lang.nosuchmethoderror的groovy脚本进行翻译

发布于 2025-01-28 21:11:09 字数 2751 浏览 6 评论 0原文

我正在尝试编写一个Groovy脚本以获取Google Translate API的翻译(使用API​​密钥,存储在Google_api_key Environment varible1中),但是我会发现一个错误,我无法克服。 Groovy脚本在

参考: google translate的API文档 Google Apis的GitHub repo

这是我的脚本:

@Grapes([
    @Grab(group='com.google.cloud', module='google-cloud-translate', version='2.1.13')
])

import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;

// api key is stored in the GOOGLE_API_KEY environment variable 
Translate translate = TranslateOptions.getDefaultInstance().getService();

String translation = translate.translate("Hello world", 
          TranslateOption.sourceLanguage("en"), 
          TranslateOption.targetLanguage("es"));

console.println(translation); // "Hola mundo"

当我运行脚本时,我会收到以下错误:

The script "/home/pico/.omegat/scripts/mt_google.groovy" is running...
An error occurred
java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;

我已经在网上阅读,这可能是由于依赖关系冲突而引起的,并且可能与guava有关。我尝试将这样的图书馆排除在外(脚本的顶部),但无济于事:

@GrabExclude(group='com.google.guava', module='guava')

预先感谢您的建议。


更新:

从我的Groovy脚本运行的Java应用程序的源代码文件夹中,我可以生成番石榴依赖性树,就像这样:

> ./gradlew dependencies | grep guava
     +--- com.google.guava:guava:23.6-jre
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    |    \--- com.google.guava:guava:15.0 -> 16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    |    \--- com.google.guava:guava:15.0 -> 16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0

我可以共享完整列表而无需Greppping,但这是一个长列表。

现在,我已经在脚本的顶部尝试了以下部分:

@Grapes([
    @GrabExclude(group='com.google.guava', module='guava', version='16.0.1'),
    @Grab(group='com.google.cloud', module='google-cloud-translate', version='2.1.13')
])

但是后来我获得了错误java.lang.noclassdeffounderror:com/google/google/common/base/base/moreobjects

I am trying to write a Groovy script to get translations from Google Translate API (using an API key, stored in as the GOOGLE_API_KEY environment variable1) but I get an error I'm not able to overcome. The Groovy script runs in OmegaT, which is a Java application.

References: Google Translate's API documentation and the code in Google APIs' Github repo.

This is my script:

@Grapes([
    @Grab(group='com.google.cloud', module='google-cloud-translate', version='2.1.13')
])

import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;

// api key is stored in the GOOGLE_API_KEY environment variable 
Translate translate = TranslateOptions.getDefaultInstance().getService();

String translation = translate.translate("Hello world", 
          TranslateOption.sourceLanguage("en"), 
          TranslateOption.targetLanguage("es"));

console.println(translation); // "Hola mundo"

When I run the script I get the following error:

The script "/home/pico/.omegat/scripts/mt_google.groovy" is running...
An error occurred
java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;

I've read online that this can be caused by a dependency conflict and that it might be related to guava. I have tried excluding that library like this (at the top of the script), but to no avail:

@GrabExclude(group='com.google.guava', module='guava')

Thanks in advance for any advice.


UPDATE:

From the source code folder of the Java application inside which my Groovy script runs I can generate a tree of Guava dependencies, like so:

> ./gradlew dependencies | grep guava
     +--- com.google.guava:guava:23.6-jre
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    |    \--- com.google.guava:guava:15.0 -> 16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    |    \--- com.google.guava:guava:15.0 -> 16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0.1
|    |    +--- com.google.guava:guava:16.0

I can share the full list without grepping but it's a long list.

I have now tried with the following section at the top of my script:

@Grapes([
    @GrabExclude(group='com.google.guava', module='guava', version='16.0.1'),
    @Grab(group='com.google.cloud', module='google-cloud-translate', version='2.1.13')
])

but then I get error java.lang.NoClassDefFoundError: com/google/common/base/MoreObjects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文