0

I followed all the steps in https://cloud.google.com/vision/docs/ocr and when executing the code I get the following Exception.

Full Stacktrace:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
    at io.grpc.Metadata$Key.validateName(Metadata.java:742)
    at io.grpc.Metadata$Key.<init>(Metadata.java:750)
    at io.grpc.Metadata$Key.<init>(Metadata.java:668)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:959)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:954)
    at io.grpc.Metadata$Key.of(Metadata.java:705)
    at io.grpc.Metadata$Key.of(Metadata.java:701)
    at com.google.api.gax.grpc.GrpcHeaderInterceptor.<init>(GrpcHeaderInterceptor.java:60)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:228)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1500(InstantiatingGrpcChannelProvider.java:71)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:202)
    at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:209)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:192)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:155)
    at com.google.cloud.vision.v1.stub.GrpcImageAnnotatorStub.create(GrpcImageAnnotatorStub.java:117)
    at com.google.cloud.vision.v1.stub.ImageAnnotatorStubSettings.createStub(ImageAnnotatorStubSettings.java:156)
    at com.google.cloud.vision.v1.ImageAnnotatorClient.<init>(ImageAnnotatorClient.java:136)
    at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:117)
    at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:108)

I tried using different Guava (22.0 and 23.6) and HttpCore (5.0 and 4.4.8) versions than the ones already being used on the Google Cloud Platform Libraries (28.2 and 4.4.12) but got no luck.

I'm using Eclipse and used the Eclipse and followed these steps: https://cloud.google.com/eclipse/docs/libraries

  • Would you share your pom.xml? I'm interested in dependency and dependencyManagement section. – suztomo Apr 07 '20 at 20:54
  • here is the pom.xml: https://jsfiddle.net/2m438wfd/ – Nicolas Guffanti Apr 08 '20 at 14:35
  • I was able to make this run downloading the whole project from GitHub, see last answer. – Nicolas Guffanti Apr 08 '20 at 14:35
  • I don't see you're using the GCP Libraries BOM. Document: https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM . Basically, with the BOM, you don't need to set individual versions of the libraries. – suztomo Apr 08 '20 at 14:37
  • Yes, I added those lines to the pom.xml file, but I still was unable to run it correctly. I really don't know what was wrong, and was able to fix it cloning the whole vision project. – Nicolas Guffanti Apr 08 '20 at 14:40
  • Can you share that pom.xml? > I added those lines to the pom.xml file – suztomo Apr 08 '20 at 14:43
  • This is my pom.xml: https://jsfiddle.net/mu49650w/, and the Effective POM is here: https://jsfiddle.net/2m438wfd/ – Nicolas Guffanti Apr 08 '20 at 16:14
  • Your pom seem correctly using the BOM. I will try to reproduce your issue tomorrow. – suztomo Apr 10 '20 at 04:29
  • I checked the pom.xml. GCP Libraries BOM has Guava 28.2-android. That version of Guava has `Preconditions.checkArgument(boolean, String, char, Object)`. Your error message says otherwise. Very likely the pom.xml is not the one when you got the NoSuchMethodError. Would you ensure you're using 28.2-android? With your IDE, look for class `com.google.common.base.Preconditions` class. Which JAR file contains the class? – suztomo Apr 10 '20 at 15:40

3 Answers3

0

As you noted, this kind of errors typically indicate version mismatches. You haven't said whether you're using Maven or the Cloud Tools for Eclipse native project. I'd recommend you use a Maven setup so that you can take advantage of the Cloud Libraries BOM. That should eliminate these version mismatches.

Brian de Alwis
  • 2,814
  • 2
  • 18
  • 32
  • "I'm using Eclipse and used the Eclipse and followed these steps: https://cloud.google.com/eclipse/docs/libraries" I tried to say that I was using the Eclipse Tools and was actually following the guide on that link that indicates how to use Eclipse Tools. I will try Maven and get back to you... Thanks – Nicolas Guffanti Apr 04 '20 at 21:36
  • Ok, just tried with Maven and I'm getting the same error. The only difference is that it made me execute it with JAVA 1.7. I was using 1.8. But the error is still present. – Nicolas Guffanti Apr 05 '20 at 02:31
  • Could you please tell me the version of the book you’re using? – Brian de Alwis Apr 05 '20 at 23:07
  • What do you mean by "book"? Regarding Cloud Vision, I'm using the latest available today (1.99.2), one of it dependencies is guava-28.2-android. I guess is something else, it can't be versions mismatch, I tried a lot of different guava versions... – Nicolas Guffanti Apr 06 '20 at 00:30
  • Sorry, I meant "bom". Helpful autocorrect to the rescue. – Brian de Alwis Apr 07 '20 at 01:31
  • I was using 4.4.1, upgraded to 4.4.2 and still having the same error... I guess the problem is somewhere else, but I don't know what else to look. – Nicolas Guffanti Apr 07 '20 at 15:11
0

That stacktrace indicates your classpath includes an old version of Guava, or the OCR client, or both. Without a complete project to inspect, I can't say exactly how you're getting that old version--it depends on how you've configured the project--but that is definitely what's happening.

0

Ok, I made this run cloning the whole java vision project from GitHub: https://github.com/googleapis/java-vision.

However I still had to add some extra jars since path errors existed.

  • You don't need to clone the library repository to use the library. Adding the dependency to your pom is sufficient. – suztomo Apr 08 '20 at 14:40
  • It was the only way to make it run... Actually I even had to modify the cloned repo, sin ce in ImageAnnotatorClient class it requested to implement AutoClosable, which was not implementing. – Nicolas Guffanti Apr 08 '20 at 16:16