4

I'm trying to generated java classes from protobuf.

Below is my protobuf -

syntax = "proto2";

package part2;

option java_package = "part2";

message OnlineStoreUser {
   required int32 userid = 1;
   required string username = 2;
   optional string useremail = 3;
}

I run the command main/exec/protoc --java_out=main/java main/proto/datamodel.proto

The protoc is downloaded from link https://github.com/protocolbuffers/protobuf/releases/tag/v3.7.1 where osx-x86_64.zip. I tried version 3.8.0 and 3.9.1 as well.

First error is https://gist.github.com/rajcspsg/07c1ddb889410397ba6fc6f26ab2b158#file-gistfile1-txt-L78. The argument to this function UnusedPrivateParameter type is not found.

Second error is https://gist.github.com/rajcspsg/07c1ddb889410397ba6fc6f26ab2b158#file-gistfile1-txt-L890. The is no 2 arg overloaded version of internalBuildGeneratedFileFrom.

What is wrong with my proto file. How can I fix this issue?

user51
  • 8,843
  • 21
  • 79
  • 158

2 Answers2

3

Make sure the dependency that you're using in your code is the same as the one that you used as protobuf compiler, for example, here I'm using gradle:

compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.11.1'

And the for my compiler version is:

✗ protoc --version                                                
libprotoc 3.11.1

Here's my test event:

syntax = "proto2";

option java_package = "com.github.irvifa.protobuf.schema";

message TestEvent {
    optional string event_name = 1 [default = "test.event"];

    optional string name = 2;
    optional string email = 3;
}

And this works.

irvifa
  • 1,865
  • 2
  • 16
  • 20
  • This is the solution, just make sure you are using the same version of probuf when you generate the class and in your maven/gradle project dependencies – phcaze Aug 25 '20 at 08:57
  • Can you please mark this as the answer then? Thanks :) – irvifa Aug 25 '20 at 11:42
2

The errors you mention only happen for me with version 3.9.1. Version 3.7.1 generates a file that doesn't use the UnusedPrivateParameter or the version of internalBuildGeneratedFileFrom with 2 parameters. It rather uses the version with 3 parameters, which does not cause any errors.

Please check that you are using the correct protoc version:

$ main/exec/protoc --version
libprotoc 3.7.1
Maik
  • 3,419
  • 1
  • 23
  • 34