使用Google Translate API获得java.lang.nosuchmethoderror的groovy脚本进行翻译
我正在尝试编写一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论