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

# Java — Containers & VMs

> Instrument a Java application with OpenTelemetry and send traces, metrics, and logs to Trace0.

<Note>
  This guide covers Java applications running on **ECS**, **EC2**, **Kubernetes**, or any container/VM environment. For Java on AWS Lambda, see the [Java Lambda guide](/setup/java/lambda).
</Note>

## 1. Install the OpenTelemetry Java Agent

Download the latest `opentelemetry-javaagent.jar` from the [OpenTelemetry Java instrumentation releases](https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases) and attach it to your JVM startup:

```bash theme={null}
java -javaagent:path/to/opentelemetry-javaagent.jar -jar myapp.jar
```

Alternatively, if you are using `Gradle`, you can use the [Gradle Javaagent Plugin](https://github.com/ryandens/javaagent-gradle-plugin#opentelemetry-integration) to attach the agent to your application.

For full configuration options, see the official [OpenTelemetry Java Agent docs](https://opentelemetry.io/docs/zero-code/java/agent/).

## 2. Set Environment Variables

Add the following environment variables to your application. Replace `YOUR_TRACE0_ENV_API_KEY` with the API key from your Trace0 environment.

| Variable                      | Value                               |
| ----------------------------- | ----------------------------------- |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `https://app.trace0hq.com/api`      |
| `OTEL_EXPORTER_OTLP_HEADERS`  | `X-API-KEY=YOUR_TRACE0_ENV_API_KEY` |

Also set the following additional environment variable for your cloud provider.

<Tabs>
  <Tab title="AWS">
    | Variable                              | Value  |
    | ------------------------------------- | ------ |
    | `OTEL_RESOURCE_PROVIDERS_AWS_ENABLED` | `true` |
  </Tab>

  <Tab title="GCP">
    | Variable                              | Value  |
    | ------------------------------------- | ------ |
    | `OTEL_RESOURCE_PROVIDERS_GCP_ENABLED` | `true` |
  </Tab>

  <Tab title="Azure">
    | Variable                                | Value  |
    | --------------------------------------- | ------ |
    | `OTEL_RESOURCE_PROVIDERS_AZURE_ENABLED` | `true` |
  </Tab>
</Tabs>

Alternatively, you can set all these environment variables in a YAML file using [declarative configuration](https://opentelemetry.io/docs/zero-code/java/agent/declarative-configuration/).

## 3. Kubernetes Setup

If your application runs on Kubernetes, including `Amazon EKS`, `Google Kubernetes Engine` or `Azure Kubernetes Service`, add the following environment variables to your container specification.

```yaml theme={null}
env:
  - name: K8S_POD_UID
    valueFrom:
      fieldRef:
        fieldPath: metadata.uid

  - name: K8S_POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name

  - name: K8S_NAMESPACE_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace

  - name: K8S_CONTAINER_NAME
    value: my-container-name

  - name: OTEL_RESOURCE_ATTRIBUTES
    value: k8s.pod.uid=$(K8S_POD_UID),k8s.pod.name=$(K8S_POD_NAME),k8s.namespace.name=$(K8S_NAMESPACE_NAME),k8s.container.name=$(K8S_CONTAINER_NAME)
```

## 4. Deploy Service

Deploy your service and watch traces, metrics, and logs appear in the [Trace0 dashboard](https://app.trace0hq.com) within seconds.

## Example Service

See our [Java ECS example app](https://github.com/Trace0-HQ/trace0-examples/tree/main/java-javalin-ecs) and [Java EC2 example app](https://github.com/Trace0-HQ/trace0-examples/tree/main/java-javalin-ec2) for working services you can deploy to your AWS account.
