
Grpc-java is the Java implementation of gRPC, maintained in its own repository and used widely on servers and on Android. It integrates with Maven and Gradle, generates idiomatic Java classes from .proto files and offers blocking, asynchronous and future-based client styles.
This guide walks through project setup, code generation, a minimal server, client calls and the production details that matter most once a service leaves your laptop.
Adding Grpc-java to a Project
A typical server project needs a transport, the Protocol Buffers integration and the stub library. The Netty-based transport is common on servers, while OkHttp is the usual choice for Android applications.
| Artefact | Purpose |
|---|---|
| io.grpc:grpc-netty-shaded | Netty transport with shaded dependencies |
| io.grpc:grpc-protobuf | Protocol Buffers marshalling support |
| io.grpc:grpc-stub | Stub classes used by generated code |
| io.grpc:grpc-okhttp | Lightweight transport, common on Android |
Use the version listed in the latest grpc-java release notes and keep all io.grpc artefacts on the same version. A bill of materials, grpc-bom, helps Maven and Gradle builds keep those versions aligned automatically.
Generating Java Code From a Proto File
Code generation runs through build plugins rather than manual commands. With Gradle, the com.google.protobuf plugin downloads protoc and the protoc-gen-grpc-java plugin, then compiles .proto files under src/main/proto during the build.
Maven projects use the protobuf-maven-plugin together with os-maven-plugin, which detects the operating system so that the correct native compiler is selected. Both approaches place generated sources in the build directory, keeping them out of version control.
What Gets Generated
For a service named Greeter, the generator creates a GreeterGrpc class containing an abstract GreeterImplBase for servers and several stub factories for clients. Message classes such as HelloRequest come from the standard Protocol Buffers Java generator.
Generated stubs are thread-safe, so one blocking or asynchronous stub can be shared across a whole application, provided that each call sets its own deadline.
Implementing the Server
A server extends the generated base class and overrides each method. Responses are delivered through a StreamObserver, which supports both single replies and streams by calling onNext one or more times followed by onCompleted.
static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> obs) {
obs.onNext(HelloReply.newBuilder().setMessage("Hello " + req.getName()).build());
obs.onCompleted();
}
}
The server is created with ServerBuilder.forPort(50051), registered services and a call to start(). Add a shutdown hook that calls shutdown() and waits for termination, so that in-flight calls can finish during deployments.
Calling the Service From a Client
Clients create a ManagedChannel pointing at the server address, then build a stub from it. Plaintext channels suit local development only; production channels should use TLS credentials.
newBlockingStubwaits for each reply and suits simple scripts and tests.newStubis fully asynchronous and required for client or bidirectional streaming.newFutureStubreturns aListenableFuturefor composition with other asynchronous code.
Reuse a channel for many calls instead of creating one per request. Channels manage connections, load balancing and reconnection internally, and they are designed to be long-lived and shared across threads.
Deadlines, Errors and Interceptors
Every call should carry a deadline, set with withDeadlineAfter on the stub. Without one, a call to an unresponsive server can wait indefinitely and tie up threads that the rest of the application needs.
Failures surface as StatusRuntimeException, whose status code explains what went wrong. Interceptors on the client and server provide a single place for logging, metrics, authentication headers and tracing, keeping that logic out of each service method.
Testing Grpc-java Services
The in-process transport lets tests start a server and client within the same JVM without opening network ports. Tests run quickly and avoid port conflicts in continuous integration environments.
Combine the in-process server with the grpc-testing utilities to clean up resources automatically after each test. This approach also makes it simple to inject fake dependencies into service implementations.
Choosing a Transport and Executor
Transport choice affects performance and compatibility. Netty suits servers and desktop applications, OkHttp suits Android, and the in-process transport serves tests and calls within one JVM.
By default, servers run service methods on a shared executor. Supplying a custom executor lets you size thread pools for your workload and keep slow handlers from starving others, which matters when services call databases or other remote systems.
Securing Channels With TLS
Production channels should use TLS rather than plaintext. Grpc-java provides credential APIs that load certificates and private keys, and the same mechanism supports mutual TLS when both client and server must prove their identity.
Token-based authentication typically travels as metadata. A client interceptor can attach a bearer token to every call, while a server interceptor validates it before the request reaches business logic.
Observability in Java Services
Interceptors are the natural place to record call counts, latency and status codes. Grpc-java also offers integrations for popular metrics and tracing systems, so instrumentation can often be added without changing service code.
- Record method names and status codes for every call.
- Measure latency on both client and server sides.
- Propagate trace context through metadata between services.
Upgrading Grpc-java Safely
Upgrades are usually smooth when versions stay aligned. Update the bill of materials, regenerate code with the matching compiler plugin and run the full test suite before deploying.
Read the release notes for deprecated APIs and changed defaults. Addressing deprecation warnings early keeps future upgrades small and predictable rather than forcing several changes at once.
Frequently Asked Questions
Does Grpc-java work on Android?
Yes. Android applications commonly use the OkHttp transport together with the lite variant of Protocol Buffers to keep application size small.
Should I use Kotlin stubs instead?
Kotlin projects can use grpc-kotlin, which builds on Grpc-java and provides coroutine-based stubs that feel more natural in Kotlin code.
Why do my generated classes fail to compile?
Mismatched versions between protoc, the Protocol Buffers runtime and the gRPC plugin are the usual cause. Align versions through the bill of materials and rebuild.
Conclusion
Grpc-java offers a mature, well-documented path to building gRPC services in Java and on Android. Build plugins handle code generation, generated base classes keep server code concise, and several stub styles fit different concurrency needs.
Keep artefact versions aligned, reuse channels, set deadlines on every call and test with the in-process transport. These practices carry a simple Greeter example smoothly into a reliable production service.


