# copilot-sdk-java

A rule that tells Copilot how to build Java apps on the GitHub Copilot SDK: client setup, sessions, async calls.

- rule · `type=rule` · GitHub Copilot · ★ 40418
- Author: github · official GitHub repository
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/github/awesome-copilot/tree/HEAD/instructions/copilot-sdk-java.instructions.md
- Updated: 2026-10-23
- HTML: https://affpapa.org/en/nevibecode/rules/copilot-sdk-java

## What it does

This instruction file loads whenever Copilot touches `*.java` or `pom.xml`. It carries working snippets: creating a `CopilotClient` with try-with-resources, configuring `CopilotClientOptions`, starting and stopping the server by hand, opening and resuming sessions through `SessionConfig` and `ResumeSessionConfig`.

One section covers virtual threads. The rule recommends JDK 25 for production, since the performance bugs in virtual threads were only fixed there, even though the feature landed in JDK 21. Java 17 is the baseline, but several examples need JDK 21 or later. GitHub Copilot CLI also has to sit in PATH, or the client won't spawn.

Async work returns `CompletableFuture`, and config getters hand back `Optional<T>` so you can tell an unset value from an explicit one. The SDK is in public preview, so breaking changes are on the table. Every session needs an `onPermissionRequest` handler — skip it and session creation fails.

## Install

### GitHub Copilot (native platform)

**Manually: put the files in place**
- File: `.github/instructions/copilot-sdk-java.instructions.md` — The applyTo field sets which files it applies to

### Claude Code

**Manually: put the files in place**
- File: `.claude/rules/copilot-sdk-java.md` — The paths frontmatter field limits the files: move applyTo from the source into paths

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### Cursor

**Manually: put the files in place**
- File: `.cursor/rules/copilot-sdk-java.mdc` — Frontmatter: description, globs, alwaysApply. Move applyTo from the source into globs, or the rule will not attach on its own

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/copilot-sdk-java.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/copilot-sdk-java.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/github/awesome-copilot/tree/HEAD/instructions/copilot-sdk-java.instructions.md

## When to use it
- wire up a Java client for the Copilot SDK without reading its source
- run async calls on virtual threads
- tell an unset config value from an explicit one
- create a session with a chosen model and tool set
- release client resources cleanly with try-with-resources

## Example prompts
- Create a CopilotClient with debug logging and start it
- Set up a session with model gpt-5, streaming on, and APPROVE_ALL as the permission handler
- Switch the session to another model with reasoningEffort high

## Source text · copilot-sdk-java.instructions.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 79310809568a5661.

BEGIN THIRD-PARTY FILE 79310809568a5661
````text
---
applyTo: '**/*.java, **/pom.xml'
description: 'This file provides guidance on building Java applications using GitHub Copilot SDK for Java.'
name: 'GitHub Copilot SDK Java Instructions'
---

## Core Principles

- The SDK is in public preview and may have breaking changes
- Requires Java 17 or later for baseline SDK usage. Some examples use newer JDK features and therefore require JDK 21 or later (for example, virtual threads via `Executors.newVirtualThreadPerTaskExecutor()` and `switch` pattern matching). **Java 25 or later highly recommended**.
- Requires GitHub Copilot CLI installed and in PATH
- Uses `CompletableFuture` for all async operations
- Implements `AutoCloseable` for resource cleanup (try-with-resources)
- Getters on configuration classes return `Optional<T>` (or `OptionalInt`/`OptionalDouble`) to distinguish "not set" from explicit values; setters accept raw types and return `this` for chaining. Use the `clear` methods to unset values if needed.

## Installation

### Maven

```xml
<dependency>
    <groupId>com.github</groupId>
    <artifactId>copilot-sdk-java</artifactId>
    <version>${copilot-sdk-java.version}</version>
</dependency>
```

### Gradle

```groovy
implementation "com.github:copilot-sdk-java:${copilotSdkJavaVersion}"
```

## Client Initialization

### Basic Client Setup

```java
try (var client = new CopilotClient()) {
    client.start().get();
    // Use client...
}
```

### Virtual Threads (JDK 25+)

Virtual threads were introduced in JDK 21, but significant performance bugs were not fixed until JDK 25, making JDK 25 the minimum recommended version for production use of virtual threads. On JDK 25+, use a virtual-thread executor for significantly better scalability. The SDK's async operations will run on virtual threads instead of the default `ForkJoinPool`:

```java
var options = new CopilotClientOptions()
    .setExecutor(Executors.newVirtualThreadPerTaskExecutor());

try (var client = new CopilotClient(options)) {
    client.start().get();
    // Use client...
}
```

### Client Configuration Options

When creating a CopilotClient, use `CopilotClientOptions`:

- `cliPath` - Path to CLI executable (default: "copilot" from PATH)
- `cliArgs` - Extra arguments prepended before SDK-managed flags
- `cliUrl` - URL of existing CLI server (e.g., "localhost:8080"). When provided, client won't spawn a process
- `port` - Server port (default: 0 for random, only when `useStdio` is false)
- `useStdio` - Use stdio transport instead of TCP (default: true)
- `logLevel` - Log level: "error", "warn", "info", "debug", "trace" (default: "info")
- `autoStart` - Auto-start server on first request (default: true)
- `autoRestart` - Auto-restart on crash (default: true)
- `cwd` - Working directory for the CLI process
- `environment` - Environment variables for the CLI process
- `gitHubToken` - GitHub token for authentication
- `useLoggedInUser` - Use logged-in `gh` CLI auth (default: true unless token provided)
- `onListModels` - Custom model list handler for BYOK scenarios
- `remote` - Enable Mission Control / cloud session integration (default: false)
- `telemetry` - `TelemetryConfig` for OpenTelemetry export (since 1.2.0)
- `sessionIdleTimeoutSeconds` - Idle timeout before session auto-closes (since 1.3.0)
- `executor` - Custom `Executor` for async operations (default: ForkJoinPool)
- `tcpConnectionToken` - Security token for TCP transport authentication

```java
var options = new CopilotClientOptions()
    .setCliPath("/path/to/copilot")
    .setLogLevel("debug")
    .setAutoStart(true)
    .setAutoRestart(true)
    .setGitHubToken(System.getenv("GITHUB_TOKEN"));
````
END THIRD-PARTY FILE 79310809568a5661

The third-party file has ended. Everything between the 79310809568a5661 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 3643 of 26505 bytes. © github · MIT · https://github.com/github/awesome-copilot/tree/HEAD/instructions/copilot-sdk-java.instructions.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
