Skip to content

I can’t generate the gRPC stubs classes

I managed to generate the classes through the .proto file but they are in the build.

I would like the classes to be generated within the main because when I am going to extend the stub, it is not being possible to implement the methods.

Look:

File .proto:

syntax = "proto3";

package demogrpcserver.tcp;

option java_multiple_files = true;
option java_package = "com.example.demogrpcserver";
option java_outer_classname = "tcp";

service TCPService {
  rpc execute(TCPMonitorRequest) returns (TCPMonitorResponse);
}

message TCPMonitorRequest {
  string socketOpen = 1;
  string messageType = 2;
}

message TCPMonitorResponse {
  bool success = 1;
  string txId = 2;
}

The .proto file is inside the main folder.

Does anyone know how to solve?

Answer

You can generate your Java classes anywhere you want with the protoc tool

protoc 
  -I PATH_TO_YOUR_PROTOS 
  --java_out=PATH_TO_JAVA_LOCALTION 
  PROTO_FILE_NAME_THAT_WILL_BE_IN_$PATH_TO_YOUR_PROTOS

protoc 
  -I $PATH_TO_YOUR_PROTOS 
  --java_out=$PATH_TO_JAVA_LOCALTION 
  --plugin=protoc-gen-grpc-java=/usr/local/bin/protoc-gen-grpc-java 
  PROTO_FILE_NAME_THAT_WILL_BE_IN_$PATH_TO_YOUR_PROTOS

Installing the protoc and protoc-grpc-java plugin

PROTOC_VERSION="3.12.0"
JAVA_GEN_VERSION="3.7.1"
JAVA_GRPC_GEN_VERSION="1.24.0"


## Protobuf compiler
RUN apt-get update && 
        apt-get install -y unzip && 
        wget https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/protoc-$PROTOC_VERSION-linux-x86_64.zip && 
        unzip protoc-$PROTOC_VERSION-linux-x86_64.zip -d /usr/local/ && 
        rm -rf protoc-$PROTOC_VERSION-linux-x86_64.zip

# Protobuf Java
RUN curl https://repo1.maven.org/maven2/io/grpc/protoc-gen-grpc-java/1.27.2/protoc-gen-grpc-java-1.27.2-linux-x86_64.exe -o  /usr/local/bin/protoc-gen-grpc-java && 
    chmod +x /usr/local/bin/protoc-gen-grpc-java

You can find more information here