Search code examples
protocol-buffersgrpcgrpc-javaprotobuf-java

Grpc file is missing when using protoc to generate code


I'm learning grpc. This is a simple hello world example:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

When I generated code using gradle's "com.google.protobuf" plugin, I get:

└─proto
    └─main
        ├─grpc
        │  └─io
        │      └─grpc
        │          └─helloworld
        │                  GreeterGrpc.java
        │
        └─java
            └─io
                └─grpc
                    └─helloworld
                            HelloReply.java
                            HelloReplyOrBuilder.java
                            HelloRequest.java
                            HelloRequestOrBuilder.java
                            HelloWorldProto.java

When I generated code using protoc(protoc --plugin=protoc-gen-grpc-java --java_out=. .\src\main\proto\helloworld.proto), I get:

└─io
    └─grpc
        └─helloworld
                HelloReply.java
                HelloReplyOrBuilder.java
                HelloRequest.java
                HelloRequestOrBuilder.java
                HelloWorldProto.java

I want to know where is the GreeterGrpc.java when I used protoc.


Solution

  • The --java_out flag specifies where to store the results of the Java plugin, but you haven't specified anywhere to specify the results of the gRPC plugin.

    You should specify --grpc-java_out=. as an extra flag.

    Additionally, your plugin flag should usually specify the binary containing the gRPC plugin, e.g. --plugin=protoc-gen-grpc-java=/path/to/grpc/plugin. (I believe otherwise protoc will look for a plugin on the path, but it's clearer if you specify the binary explicitly.)

    See the gRPC Java plugin README for more details.