7

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?

Lii
  • 11,553
  • 8
  • 64
  • 88
Richa Gupta
  • 143
  • 3
  • 10
  • I just want to add that I already have protobuf-java-2.0.5 jar in my library root. – Richa Gupta Nov 24 '16 at 18:57
  • For some reason the compiler does not see the the protobuf library, probably an issue with the sbt build rule. Are you sure sbt uses your library root? Did you try compiling the xxx.java manually with javac and protobuf-java-2.0.5.jar in the classpath? – Stefan Haustein Nov 24 '16 at 21:48
  • Yes, javac xxx.java -cp $PATH_TO_Protobuf_jar/protobuf-java-2.5.0.jar But still it gives errors -xxx.java:6188: error: package com.google.protobuf.GeneratedMessageV3 does not exist com.google.protobuf.GeneratedMessageV3.FieldAccessorTable ^ xxx.java:6193: error: package com.google.protobuf.GeneratedMessageV3 does not exist com.google.protobuf.GeneratedMessageV3.FieldAccessorTable – Richa Gupta Nov 25 '16 at 08:05
  • Is there a newer version available? The "V3" suffix seems to suggest you'd need protobuf-java-3.0.0.jar or later? – Stefan Haustein Nov 25 '16 at 16:46

3 Answers3

7

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.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
1

Re-stating Kenton's answer with some more instructions:

In Intellij, click on External Libraries and find the jar for protobuf.

enter image description here

Check the version of protoc:

enter image description here

If they don't match, (as shown above) then you will get the compilation errors.

Janac Meena
  • 3,203
  • 35
  • 32
0

I've seen similar issue with maven after changing some field type in my proto schema and then building without doing a clean first. However, doing a clean and build fixed it every time.

magician
  • 547
  • 4
  • 7