Richa Gupta
Richa Gupta

Reputation: 143

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?

Upvotes: 7

Views: 11530

Answers (3)

magician
magician

Reputation: 617

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.

Upvotes: 0

Janac Meena
Janac Meena

Reputation: 3567

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.

Upvotes: 1

Kenton Varda
Kenton Varda

Reputation: 45171

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.

Upvotes: 7

Related Questions