Get started with the JRE Java SDK

このページはまだあなたの言語に翻訳されていません。 現在取り組んでいますので、後でもう一度確認してください。

For Java and JVM developers10 minBeginner

This guide helps you install the JRE Java SDK (com.amplitude:java-sdk), initialize it, and send one event from a JVM application to Amplitude. Each step lists the conditions that commonly break that step, so you can fix a problem where it happens. For every setter, go to Configuration options. For changes between versions, go to the Java SDK changelog.

The JRE Java SDK is for server-side and desktop JVM code, including Kotlin and Scala. The SDK doesn't store a user ID, device ID, or session between calls, and it collects no device data. Set identity and context on each event. For Android apps, use the Android-Kotlin SDK instead.

Prerequisites

  • An Amplitude project and its API key. Find the key in Settings > Projects > your project > General.
  • The data center your organization uses: US (default) or EU.
  • Java 8 or later. The SDK compiles to Java 8 bytecode. The examples on these pages run on Java 17.
  • The latest SDK release, or a pinned version you check against the changelog for the features you use.

Install the SDK

Add the SDK and org.json. The SDK declares org.json as a runtime dependency only. Your code can't use JSONObject, the type of event and user properties, unless you add org.json yourself. Use version 20231013 or later, the version the SDK compiles against.

groovy
dependencies {
    implementation 'com.amplitude:java-sdk:1.13.1'
    implementation 'org.json:json:20231013'
}

Fix installation problems

Initialize the SDK

Get an instance, then call init() with your API key once, when your application starts.

java
import com.amplitude.Amplitude;

Amplitude client = Amplitude.getInstance();
client.init("AMPLITUDE_API_KEY");

getInstance() returns the same object every time you call it, so other classes can call Amplitude.getInstance() instead of passing the client around.

If your project uses the EU data center, set the EU endpoint with setServerUrl() after init():

java
Amplitude client = Amplitude.getInstance();
client.init("AMPLITUDE_API_KEY");
client.setServerUrl("https://api.eu.amplitude.com/2/httpapi");

client.logEvent(new Event("Sign Up Completed", "user-12345"));

Fix initialization problems

Send a first event

Log one event for a user, then flush and shut down before the program ends. Pass the user's ID to the Event constructor, because the SDK doesn't remember identity between calls.

java
import com.amplitude.Amplitude;
import com.amplitude.Event;

public class FirstEvent {
  public static void main(String[] args) throws InterruptedException {
    Amplitude client = Amplitude.getInstance();
    client.init("AMPLITUDE_API_KEY");

    Event event = new Event("Sign Up Completed", "user-12345");
    event.addEventProperty("plan", "starter");
    client.logEvent(event);

    client.flushEvents();
    client.shutdown();
  }
}

logEvent() only adds the event to a queue in memory. The SDK uploads the queue every 10 seconds, or when it holds 10 events. flushEvents() starts the upload now, and shutdown() stops the SDK's threads. After an upload, the JVM can take up to 60 seconds to exit, because it waits for an idle SDK thread to time out. To exit sooner, go to Flush and shut down.

Fix upload problems

To see the result of each upload, set a callback. Go to Read the upload result.

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the user ID you passed, user-12345 in the example.
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Completed appears with the plan property.

If the event doesn't appear within a minute, follow Verify events, which checks each stage between logEvent() and Amplitude's servers.

Next steps

これは役に立ちましたか?

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

このページでは