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

# Java SDK

> Install and configure Sentry in Java and Kotlin applications

Sentry provides two Java SDK artifacts:

* **`sentry`** — for plain Java or Kotlin applications and server-side frameworks.
* **`sentry-android`** — for Android apps. Extends `sentry` with ANR detection, activity lifecycle tracking, network monitoring, and more.

## Installation

<Tabs>
  <Tab title="Maven">
    ```xml theme={null}
    <dependency>
        <groupId>io.sentry</groupId>
        <artifactId>sentry</artifactId>
        <version>7.0.0</version>
    </dependency>
    ```
  </Tab>

  <Tab title="Gradle (JVM)">
    ```groovy theme={null}
    implementation 'io.sentry:sentry:7.0.0'
    ```
  </Tab>

  <Tab title="Gradle (Android)">
    ```groovy theme={null}
    implementation 'io.sentry:sentry-android:7.0.0'
    ```

    No manual `Sentry.init()` is required on Android if you use the auto-init feature. Add your DSN to `AndroidManifest.xml`:

    ```xml theme={null}
    <application>
        <meta-data
            android:name="io.sentry.dsn"
            android:value="https://examplePublicKey@o0.ingest.sentry.io/0" />
        <meta-data
            android:name="io.sentry.traces-sample-rate"
            android:value="1.0" />
    </application>
    ```
  </Tab>

  <Tab title="Spring Boot">
    ```xml theme={null}
    <dependency>
        <groupId>io.sentry</groupId>
        <artifactId>sentry-spring-boot-starter-jakarta</artifactId>
        <version>7.0.0</version>
    </dependency>
    ```

    Then add to `application.properties`:

    ```properties theme={null}
    sentry.dsn=https://examplePublicKey@o0.ingest.sentry.io/0
    sentry.traces-sample-rate=1.0
    sentry.environment=production
    ```
  </Tab>
</Tabs>

## Basic initialization

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    import io.sentry.Sentry;

    public class Application {
        public static void main(String[] args) {
            Sentry.init(options -> {
                options.setDsn("https://examplePublicKey@o0.ingest.sentry.io/0");
                options.setEnvironment("production");
                options.setRelease("my-app@1.0.0");
                options.setTracesSampleRate(1.0);
            });

            // Your application code here
        }
    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    import io.sentry.Sentry

    fun main() {
        Sentry.init { options ->
            options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
            options.environment = "production"
            options.release = "my-app@1.0.0"
            options.tracesSampleRate = 1.0
        }

        // Your application code here
    }
    ```
  </Tab>

  <Tab title="Android (Kotlin)">
    For custom initialization (overrides manifest-based auto-init):

    ```kotlin theme={null}
    import io.sentry.android.core.SentryAndroid

    class MyApplication : Application() {
        override fun onCreate() {
            super.onCreate()
            SentryAndroid.init(this) { options ->
                options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
                options.environment = "production"
                options.release = BuildConfig.VERSION_NAME
                options.tracesSampleRate = 1.0
                options.profilesSampleRate = 1.0
            }
        }
    }
    ```
  </Tab>
</Tabs>

<Note>
  Initialize Sentry in your `main` method (JVM) or `Application.onCreate()` (Android) before any other code runs. This ensures errors during startup are captured.
</Note>

## Capturing exceptions

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    try {
        processOrder(cart);
    } catch (Exception e) {
        Sentry.captureException(e);
        throw e;
    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    try {
        processOrder(cart)
    } catch (e: Exception) {
        Sentry.captureException(e)
        throw e
    }
    ```
  </Tab>
</Tabs>

### Capture a message

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    Sentry.captureMessage("Inventory below threshold", SentryLevel.WARNING);
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    Sentry.captureMessage("Inventory below threshold", SentryLevel.WARNING)
    ```
  </Tab>
</Tabs>

## Setting context

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    Sentry.configureScope(scope -> {
        scope.setUser(new User());
        scope.getUser().setId("42");
        scope.getUser().setEmail("user@example.com");
        scope.setTag("section", "checkout");
        scope.setExtra("orderId", "12345");
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    Sentry.configureScope { scope ->
        scope.user = User().apply {
            id = "42"
            email = "user@example.com"
        }
        scope.setTag("section", "checkout")
        scope.setExtra("orderId", "12345")
    }
    ```
  </Tab>
</Tabs>

## Performance monitoring

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    ITransaction transaction = Sentry.startTransaction("checkout", "task");

    ISpan dbSpan = transaction.startChild("db.query", "SELECT orders");
    try {
        List<Order> orders = orderRepository.findByUser(userId);
        dbSpan.setData("row_count", orders.size());
    } finally {
        dbSpan.finish();
    }

    transaction.finish();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val transaction = Sentry.startTransaction("checkout", "task")

    val dbSpan = transaction.startChild("db.query", "SELECT orders")
    try {
        val orders = orderRepository.findByUser(userId)
        dbSpan.setData("row_count", orders.size)
    } finally {
        dbSpan.finish()
    }

    transaction.finish()
    ```
  </Tab>
</Tabs>

### Spring Boot: @SentryTransaction

With the Spring Boot starter, annotate methods to create transactions automatically:

```java theme={null}
import io.sentry.spring.jakarta.tracing.SentryTransaction;

@Service
public class OrderService {

    @SentryTransaction(operation = "task", name = "processOrder")
    public void processOrder(Order order) {
        // method body is wrapped in a Sentry transaction
    }
}
```

## Android-specific features

The `sentry-android` SDK includes capabilities beyond the standard Java SDK:

| Feature                 | Description                                                      |
| ----------------------- | ---------------------------------------------------------------- |
| **Crash reporting**     | Captures Java, Kotlin, and native (NDK) crashes.                 |
| **ANR detection**       | Reports Application Not Responding events with a thread dump.    |
| **OOM detection**       | Detects out-of-memory terminations (experimental).               |
| **Slow/frozen frames**  | Tracks UI frame rates for performance monitoring.                |
| **Session tracking**    | Measures crash-free session and user rates.                      |
| **Screenshot on crash** | Optionally attaches a screenshot taken at the moment of a crash. |
| **View hierarchy**      | Attaches a dump of the view hierarchy on crash.                  |

Enable screenshot and view hierarchy capture in your `SentryAndroid.init()`:

```kotlin theme={null}
SentryAndroid.init(this) { options ->
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    options.isAttachScreenshot = true
    options.isAttachViewHierarchy = true
}
```

<Warning>
  Screenshots may capture sensitive user data. Review your privacy policy before enabling `isAttachScreenshot` in production.
</Warning>
