Skip to main content
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

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry</artifactId>
    <version>7.0.0</version>
</dependency>

Basic initialization

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
    }
}
Initialize Sentry in your main method (JVM) or Application.onCreate() (Android) before any other code runs. This ensures errors during startup are captured.

Capturing exceptions

try {
    processOrder(cart);
} catch (Exception e) {
    Sentry.captureException(e);
    throw e;
}

Capture a message

Sentry.captureMessage("Inventory below threshold", SentryLevel.WARNING);

Setting context

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");
});

Performance monitoring

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();

Spring Boot: @SentryTransaction

With the Spring Boot starter, annotate methods to create transactions automatically:
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:
FeatureDescription
Crash reportingCaptures Java, Kotlin, and native (NDK) crashes.
ANR detectionReports Application Not Responding events with a thread dump.
OOM detectionDetects out-of-memory terminations (experimental).
Slow/frozen framesTracks UI frame rates for performance monitoring.
Session trackingMeasures crash-free session and user rates.
Screenshot on crashOptionally attaches a screenshot taken at the moment of a crash.
View hierarchyAttaches a dump of the view hierarchy on crash.
Enable screenshot and view hierarchy capture in your SentryAndroid.init():
SentryAndroid.init(this) { options ->
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    options.isAttachScreenshot = true
    options.isAttachViewHierarchy = true
}
Screenshots may capture sensitive user data. Review your privacy policy before enabling isAttachScreenshot in production.