Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation error in compiling Protobufs in Java using SBT build tool

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?

like image 778
Richa Gupta Avatar asked Nov 24 '16 18:11

Richa Gupta


2 Answers

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.

like image 127
Kenton Varda Avatar answered Oct 17 '22 22:10

Kenton Varda


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.

like image 33
Janac Meena Avatar answered Oct 17 '22 23:10

Janac Meena