Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protoc command not generating all base classes (java)

I have been trying to generate the basic gRPC client and server interfaces from a .proto service definition here from the grpc official repo. The relevant service defined in that file (from the link above) is below:

service RouteGuide {
    rpc GetFeature(Point) returns (Feature) {}
    rpc ListFeatures(Rectangle) returns (stream Feature) {}
    rpc RecordRoute(stream Point) returns (RouteSummary) {}
    rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}

The command I run is protoc --java_out=${OUTPUT_DIR} path/to/proto/file

According to the grpc site (specifically here), a RouteGuideGrpc.java which contains a base class RouteGuideGrpc.RouteGuideImplBase, with all the methods defined in the RouteGuide service is supposed to have been generated from the protoc command above, but that file does not get generated for me.

Has anyone faced similar issues? Is the official documentation simply incorrect? And would anyone have any suggestion as to what I can do to generate that missing class?

like image 358
shafeen Avatar asked Dec 30 '18 09:12

shafeen


People also ask

What does protoc command do?

The protoc command will generate Java output file from our addressbook. proto file. The -I option specifies a directory in which a proto file resides. The java-out specifies a directory where the generated class will be created.

How do I change the default value in proto?

Update: The below answer is for proto2 only, proto3 doesn't allow custom default values. Yes, you can give default values as you had written. default is optional for required , but for optional you have to mention the default values else type specific value is automatically assigned.

What is protoc GEN Go?

protoc-gen-go is a filter for the protoc(1) protocol buffer compiler. It takes the protocol buffer format from protoc(1) as input and produces Go code as output which can then be used to encode (marshal) and decode (unmarshal) data in the protocol buffer format specified in its input.

How do I create a Java proto file?

The protocol buffer compiler produces Java output when invoked with the --java_out= command-line flag. The parameter to the --java_out= option is the directory where you want the compiler to write your Java output. For each . proto file input, the compiler creates a wrapper .


1 Answers

This may help someone else in the future so I'll answer my own question.

I believe the java documentation for gRPC code generation is not fully up to date and the information is scattered amongst different official repositories.

So turns out that in order to generate all the gRPC java service base classes as expected, you need to specify an additional flag to the protoc cli like so grpc-java_out=${OUTPUT_DIR}. But in order for that additional flag to work, you need to have a few extra things:

  1. The binary for the protoc plugin for gRPC Java protoc-gen-grpc-java: you can get the relevant one for your system from maven central here (the link is for v1.17.1). If there isn't a prebuilt binary available for your system, you can compile one yourself from the github repo instructions here.
  2. Make sure the binary location is added to your PATH environment variable and the binary is renamed to "protoc-gen-grpc-java" exactly (that is the name the protoc cli expects to have in the path).

  3. Finally, you are ready to run the correct command protoc --java_out=${OUTPUT_DIR} --grpc-java_out=${OUTPUT_DIR} path/to/proto/file and now the service base classes like RouteGuideGrpc.RouteGuideImplBase should be generated when it previously was not.

I hope this explanation helps someone else out in the future.

like image 123
shafeen Avatar answered Sep 29 '22 15:09

shafeen