Compilation error in compiling Protobufs in Java using SBT build tool

Richa Gupta picture Richa Gupta · Nov 24, 2016 · Viewed 7.5k times · Source

I am using the Play framework (which uses SBT build tool) with Java where I need to consume a Protobuf. So I have xxx.proto file. I got binary protoc compiler and added to class path. so I see -

protoc --version
libprotoc 3.1.0

I have compiled the xxx.proto file using - protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/xxx.proto so it has generated xxx.java file.

Now when I am compiling this xxx.java file ( the project using sbt build tool)

[error] /my_project/app/helpers/xxx.java:7: package com.google.protobuf        does not exist
[error] com.google.protobuf.ExtensionRegistryLite
[error] /my_project/app/helpers/xxx.java:11: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistry
[error] /my_project/app/helpers/xxx.java:6182: package com.google.protobuf.Descriptors does not exist
[error] com.google.protobuf.Descriptors.Descriptor
[error] /my_project/app/helpers/xxx.java:6185: package com.google.protobuf.GeneratedMessageV3 does not exist
[error] com.google.protobuf.GeneratedMessageV3.FieldAccessorTable`

I see in my installed library - com.google.protobuf jar is there.

My xxx.proto looks following -

 // Generated by the protocol buffer compiler.  DO NOT EDIT!
 // source: xxx.proto

 public final class xxx {
 private xxx() {}
 public static void registerAllExtensions(
  com.google.protobuf.ExtensionRegistryLite registry) {
 }

  public static void registerAllExtensions(
  com.google.protobuf.ExtensionRegistry registry) {
   registerAllExtensions(
    (com.google.protobuf.ExtensionRegistryLite) registry);
 }
 ......

Is there anything I have missed while generating the xxx.java file? How should I fix these compilation error?

Answer

Kenton Varda picture Kenton Varda · Nov 26, 2016

You need to make sure that you're using the exact same versions of protoc and libprotobuf.jar. From what you wrote, it sounds like you're using protoc version 3.1.0 but libprotobuf 2.5.0. You need to use libprotobuf 3.1.0 instead, otherwise you will get compile errors like the ones you quote.