protoc command not generating all base classes (java)
Asked Answered
R

2

6

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?

Retha answered 30/12, 2018 at 9:49 Comment(0)
R
16

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.

Retha answered 31/12, 2018 at 0:16 Comment(2)
Instead of renaming to proto-gen-grpc-java you can also use the --plugin flag. See https://mcmap.net/q/361826/-protoc-not-generating-service-stub-filesJovia
Thanks this helped me. I want to add a point. Not sure whether this is addressed in the doc. java_out - created classes for all messages and respective builders grpc-java_out - created the base and stub classes for the servicesAmelina
P
0

Thank you very much for this investigation. Indeed, the doc is incomplete, and people use Maven to compile everything without understanding of how it really works. Yp

Prothorax answered 2/9, 2022 at 16:58 Comment(1)
This does not have any answerCollage

© 2022 - 2025 — McMap. All rights reserved.