grpc/grpc-java · error · UnsupportedOperationException

Use forPort(int, ServerCredentials) instead

Error message

Use forPort(int, ServerCredentials) instead

What it means

OkHttpServerBuilder.forPort(int) is a permanently dead static factory: it is annotated @DoNotCall and always throws UnsupportedOperationException. gRPC Java moved port+credentials APIs to take a ServerCredentials argument (for TLS etc.), so the credentials-less overload was removed. Callers must migrate to the two-argument overload.

Source

Thrown at okhttp/src/main/java/io/grpc/okhttp/OkHttpServerBuilder.java:95

  private static final long MIN_MAX_CONNECTION_IDLE_NANO = TimeUnit.SECONDS.toNanos(1L);
  static final long MAX_CONNECTION_AGE_NANOS_DISABLED = Long.MAX_VALUE;
  static final long MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE = Long.MAX_VALUE;
  static final int MAX_CONCURRENT_STREAMS = Integer.MAX_VALUE;
  private static final long MIN_MAX_CONNECTION_AGE_NANO = TimeUnit.SECONDS.toNanos(1L);

  private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000L);
  private static final ObjectPool<Executor> DEFAULT_TRANSPORT_EXECUTOR_POOL =
      OkHttpChannelBuilder.DEFAULT_TRANSPORT_EXECUTOR_POOL;

  /**
   * Always throws, to shadow {@code ServerBuilder.forPort()}.
   *
   * @deprecated Use {@link #forPort(int, ServerCredentials)} instead
   */
  @DoNotCall("Always throws. Use forPort(int, ServerCredentials) instead")
  @Deprecated
  public static OkHttpServerBuilder forPort(int port) {
    throw new UnsupportedOperationException("Use forPort(int, ServerCredentials) instead");
  }

  /**
   * Creates a builder for a server listening on {@code port}.
   */
  public static OkHttpServerBuilder forPort(int port, ServerCredentials creds) {
    return forPort(new InetSocketAddress(port), creds);
  }

  /**
   * Creates a builder for a server listening on {@code address}.
   */
  public static OkHttpServerBuilder forPort(SocketAddress address, ServerCredentials creds) {
    HandshakerSocketFactoryResult result = handshakerSocketFactoryFrom(creds);
    if (result.error != null) {
      throw new IllegalArgumentException(result.error);
    }
    return new OkHttpServerBuilder(address, result.factory);

View on GitHub (pinned to 64daddc1f3)

Solutions

  1. Replace the call with OkHttpServerBuilder.forPort(port, creds), e.g. InsecureServerCredentials.getInsecureInstance() for plaintext
  2. Alternatively use Grpc.newServerBuilderForPort(port, creds) or ServerBuilder.forPort(port, creds) for provider-based construction
  3. If plaintext is all you need, verify the imported class is the correct builder and no stale overload was resolved

Example fix

// before
OkHttpServerBuilder builder = OkHttpServerBuilder.forPort(8080);
// after
OkHttpServerBuilder builder = OkHttpServerBuilder.forPort(8080, InsecureServerCredentials.getInsecureInstance());
Defensive patterns

Strategy: validation

Validate before calling

if (useTls) {
  builder = OkHttpServerBuilder.forPort(port, TlsServerCredentials.newBuilder().keyManager(certChain, privateKey).build());
} else {
  builder = OkHttpServerBuilder.forPort(port, InsecureServerCredentials.getInsecureInstance());
}

Prevention

When it happens

Trigger: Any direct call to the deprecated static OkHttpServerBuilder.forPort(int) — e.g. old code written before the API migration — throws immediately.

Common situations: Upgrading grpc-java/okhttp to a version where the credentials-less forPort was made fatal; copy-pasted legacy server bootstrap code; tutorials or blog posts written against older gRPC versions.

Understand the failure class

Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.

Related errors


AI-assisted analysis of grpc/grpc-java@64daddc1f3 (2026-09-08). Data as JSON: /api/errors/de657abe63aed8cc. Report an issue: GitHub.