'Groovy script to get translations using Google Translate API gives java.lang.NoSuchMethodError
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
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|