46

I have just started playing with google proto. When I try to compile proto file present in proto-java example, it does not generate any grpc file.

proto file, https://github.com/grpc/grpc-java/blob/master/examples/src/main/proto/hello_world.proto

terminal output,

rsonkhla@raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc --version libprotoc 3.0.0 rsonkhla@raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc --java_out=test/ -I../../grpc-java/examples ../../grpc-java/examples/src/main/proto/hello_world.proto rsonkhla@raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ ls -R test/ test/: io

test/io: grpc

test/io/grpc: examples

test/io/grpc/examples: helloworld

test/io/grpc/examples/helloworld: HelloRequest.java
HelloResponse.java HelloWorldProto.java HelloRequestOrBuilder.java HelloResponseOrBuilder.java

Has anybody else faced this issue?

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Raman Sonkhla
  • 461
  • 1
  • 4
  • 3
  • Please ignore. I did not pass protoc-gen-grpc-java plugin path while compiling... – Raman Sonkhla Jun 24 '15 at 15:36
  • rsonkhla@raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc --plugin=protoc-gen-java_rpc=../compiler/build/binaries/java_pluginExecutable/protoc-gen-grpc-java --java_rpc_out=test/ --java_out=test/ -I. src/main/proto/hello_world.proto test/io/grpc/examples/helloworld: GreeterGrpc.java HelloRequest.java HelloRequestOrBuilder.java HelloResponse.java HelloResponseOrBuilder.java HelloWorldProto.java – Raman Sonkhla Jun 24 '15 at 15:40
  • 1
    I use protobuf-maven-plugin, It also can't generate service stub files. – Rocky Hu Jun 23 '17 at 02:23
  • 404 on that github link – Evorlor Feb 23 '22 at 20:29

2 Answers2

39

The command line you are showing does not enable the grpc plugin. You need to specify an _out argument for the grpc plugin, which enables the plugin and specifies where it should output files. Since the plugin is likely not in your PATH, you also need to tell protoc how to find the plugin with --plugin.

So you need to add two arguments:

--plugin=protoc-gen-grpc-java=path/to/protoc-gen-grpc-java --grpc-java_out=path/to/output/dir

For more info, see the gRPC compiler documentation.

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
  • 1
    Hi Eric, i'm trying to make this work, i've downloaded protoc-gen-grpc-java.jar from maven, but no matter how i feed it to that argument it will still tell me that "file not found or is not executable", maybe i shall have some other artifact instead of jar file? – vach Apr 27 '16 at 10:19
  • is your "path/to/protoc-gen-grpc-java" pointing to a jar file or something else? – vach Apr 27 '16 at 10:22
  • 6
    path/to/protoc-gen-grpc-java is a binary (e.g., a native ELF binary for Linux). It is not a JAR. You can find pre-built versions at http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.grpc%22%20a%3A%22protoc-gen-grpc-java%22 (e.g., linux-x86_32.exe, which could be used like `--plugin=protoc-gen-grpc-java=path/to/linux-x86_32.exe`) – Eric Anderson Apr 27 '16 at 21:43
  • Thanks i've already compiled it from github project, although it was generating not valid code (grpc-all.jar was missing some code which was used in generated one) so i just created a gradle mini project which only takes care of protobuf part... i hope we will have official support for sbt in future... – vach Apr 28 '16 at 03:45
  • @EricAnderson Thans, this is what I was looking for. However, ELF binary with .exe suffix, it sounds really weird ;) I didn't believe that it's really what need when I found it for the first time... – Mi-La Oct 12 '17 at 12:19
34

You can add these option to your .proto (base on your language) to generate abstract services:

option cc_generic_services = true;
option java_generic_services = true;
option py_generic_services = true;

You can also add --plugin=EXECUTABLE option in your protoc cmd to use custom code generator plugin to generate code more specific to each system, rather than rely on the "abstract" services. Just like Eric's suggestion.

djzhu
  • 787
  • 8
  • 11