> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squasher.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Java

> Send errors and logs from Java, Spring Boot, and JVM applications to Squasher.

## Recommended: OpenTelemetry Java Agent

The OTel Java agent auto-instruments your application with zero code changes. It hooks into popular frameworks (Spring Boot, Quarkus, Micronaut, Vert.x) and libraries (JDBC, Hibernate, OkHttp, gRPC).

### 1. Download the Agent

```bash theme={null}
curl -L -o opentelemetry-javaagent.jar \
  https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
```

### 2. Run Your Application

```bash theme={null}
java -javaagent:opentelemetry-javaagent.jar \
  -Dotel.exporter.otlp.endpoint=https://ingest.squasher.ai \
  -Dotel.exporter.otlp.headers="x-squasher-key=sq_pk_your_api_key" \
  -Dotel.service.name=my-java-service \
  -jar your-app.jar
```

All uncaught exceptions, error responses, and JDBC errors are automatically captured and sent to Squasher.

## Spring Boot

The OTel agent works out of the box with Spring Boot. For finer control, use the Spring Boot Starter:

```xml theme={null}
<!-- pom.xml -->
<dependency>
  <groupId>io.opentelemetry.instrumentation</groupId>
  <artifactId>opentelemetry-spring-boot-starter</artifactId>
</dependency>
```

```yaml theme={null}
# application.yml
otel:
  exporter:
    otlp:
      endpoint: https://ingest.squasher.ai
      headers:
        x-squasher-key: sq_pk_your_api_key
  service:
    name: my-spring-app
```

## Alternative: Direct HTTP API

```java theme={null}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Squasher {
    private static final String ENDPOINT = "https://ingest.squasher.ai/v1/ingest/YOUR_PROJECT_ID";
    private static final String API_KEY = "sq_pk_your_api_key";
    private static final HttpClient client = HttpClient.newHttpClient();

    public static void captureError(Throwable error, String... tags) {
        StringBuilder stack = new StringBuilder();
        for (StackTraceElement el : error.getStackTrace()) {
            stack.append("    at ").append(el.toString()).append("\n");
        }

        String json = String.format(
            "{\"message\":\"%s\",\"type\":\"%s\",\"level\":\"error\",\"stack\":\"%s\"}",
            error.getMessage().replace("\"", "\\\""),
            error.getClass().getSimpleName(),
            stack.toString().replace("\"", "\\\"").replace("\n", "\\n")
        );

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(ENDPOINT))
            .header("Content-Type", "application/json")
            .header("x-squasher-key", API_KEY)
            .POST(HttpRequest.BodyPublishers.ofString(json))
            .build();

        client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    }
}
```

### Usage

```java theme={null}
try {
    processOrder(order);
} catch (Exception e) {
    Squasher.captureError(e);
    throw e;
}
```

## Agent handoff

```text theme={null}
For Java, prefer the OpenTelemetry Java agent for framework auto-instrumentation. Keep the Squasher key in runtime configuration, set otel.service.name, verify one uncaught exception or error log, and use direct HTTP only for small custom integrations.
```
