Artanis Zeratul
Artanis Zeratul

Reputation: 1003

Setting gRPC Java

Good day to you Java and gRPC gurus out there.

I have been following this https://github.com/jpdna/gRPC-maven-helloworld, because I am learning gRPC using Java.

I was able to compile it using mvn clean package. But when I loaded the project in Eclipse, the file:

org.jpdna.grpchello.HelloWorldServer

is looking for the class "GreeterGrpc".

I tried executing this in the terminal:

$ protoc --java_out=/gRPC-maven-helloworld/src/main/java hello_world.proto

It generated the following classes:

  - HelloRequest.java
  - HelloRequestOrBuilder.java
  - HelloResponse.java
  - HelloResponseOrBuilder.java
  - HelloWorldProto.java

But no GreeterGrpc.java which is defined as a service in this project. If you don't mind me asking, I would like to know how to create or generate this GreeterGrpc.java class?

Many thanks to you guys!

Upvotes: 1

Views: 2052

Answers (2)

Artanis Zeratul
Artanis Zeratul

Reputation: 1003

I have found the answer to my question... the solution is just to right click on the project on Eclipse, then choose Run As --> Maven generate-sources. Once the source codes and its folders are generated in the target folder. Right click on those generated folders (with source codes) then, Build Path --> Use as Source Folder. The error will go away since it will be able to resolve the missing classes.

Thanks and have a good day =)!

Upvotes: 1

Z fp
Z fp

Reputation: 574

You need a protoc plugin protoc-gen-grpc-java to generate GreeterGrpc.class when complie hello_world.proto.

Or you can use a maven plugin to achieve the same thing. My maven config:

<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-all</artifactId>
    <version>1.0.3</version>
</dependency>


<extension>
    <groupId>kr.motd.maven</groupId>
    <artifactId>os-maven-plugin</artifactId>
    <version>1.5.0.Final</version>
</extension>


<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
    <execution>
        <id>uncompress</id>
        <phase>install</phase>
        <goals>
            <goal>exec</goal>
        </goals>
        <configuration>
            <executable>${basedir}/bin/upgrade.sh ${environment}</executable>
        </configuration>
    </execution>
</executions>
</plugin>


<plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <version>0.5.0</version>
    <configuration>
<protocArtifact>com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.3:exe:${os.detected.classifier}</pluginArtifact>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>compile-custom</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Upvotes: 3

Related Questions