Get started with the JRE Java SDK
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.
dependencies {
implementation 'com.amplitude:java-sdk:1.13.1'
implementation 'org.json:json:20231013'
}
Fix installation problems
| Symptom | Cause | Fix |
|---|---|---|
package org.json does not exist, or cannot find symbol: class JSONObject | Your build has only the SDK. org.json reaches the runtime classpath, not the compile classpath. | Add org.json:json:20231013 as a dependency. |
java.lang.NoClassDefFoundError: org/json/JSONObject at runtime | You added the SDK JAR by hand without json-20231013.jar. | Add the org.json JAR to the classpath. |
java.lang.UnsupportedClassVersionError | The JVM is older than Java 8. | Run on Java 8 or later. |
Initialize the SDK
Get an instance, then call init() with your API key once, when your application starts.
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():
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
| Symptom | Cause | Fix |
|---|---|---|
Flush Thread Error: ... ClassCastException: class sun.net.www.protocol.http.HttpURLConnection cannot be cast to class javax.net.ssl.HttpsURLConnection | setServerUrl() received an http:// URL. The SDK sends only over HTTPS. | Use an https:// URL. |
Events go to the default US endpoint, though you called setServerUrl() | The URL doesn't start with http:// or https://, so the SDK ignored it. | Pass a full URL, such as https://api.eu.amplitude.com/2/httpapi. |
Events go to HTTP V2, though you called useBatchMode(true) | You called useBatchMode() before init(). init() turns batch mode off. | Call useBatchMode(true) after init(). |
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.
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.
| Symptom | Cause | Fix |
|---|---|---|
IllegalArgumentException: Event must have one defined userId and/or deviceId | The Event constructor got null for both IDs. | Pass a user ID or a device ID. |
| The program never exits | The SDK sent events, and you didn't call shutdown(). The SDK's upload threads keep the JVM alive. | Call flushEvents(), then shutdown(), when the program finishes. |
About 10 seconds after shutdown(): Exception in thread "Thread-0" java.util.concurrent.RejectedExecutionException, and the event never arrives | You called shutdown() without flushEvents(). shutdown() doesn't upload the queue, and the scheduled upload finds the threads stopped. | Call flushEvents() before shutdown(). |
Flush Thread Error: ... AmplitudeInvalidAPIKeyException: Amplitude Exception: Invalid API key | The key is wrong, or belongs to a project in the other data center. | Copy the key again, and check that setServerUrl() matches the project's data center. |
Callback with 400, Request missing required field | The request had no API key, because init() received an empty or null key. | Check the environment variable or setting that holds your key. |
Callback with 400, Invalid id length for user_id or device_id | An ID is shorter than five characters, Amplitude's default minimum. | Use longer IDs, or allow shorter IDs. |
Callback with 408, Event retries exhausted. | Every request failed before a response arrived: DNS failure, a firewall, or no outbound network. | Confirm that the host can reach api2.amplitude.com (or api.eu.amplitude.com) on port 443. If traffic must go through a proxy, go to Route uploads through an HTTP proxy. |
Confirm the event in Amplitude
- In Amplitude, open User Lookup and search for the user ID you passed,
user-12345in the example. - Open the Activity tab and turn on Live event updates.
- Confirm that
Sign Up Completedappears with theplanproperty.
If the event doesn't appear within a minute, follow Verify events, which checks each stage between logEvent() and Amplitude's servers.
Next steps
- Add the events and properties your tracking plan needs. Go to Track events.
- Flush events before a job, script, or serverless function exits. Go to Flush and shut down.
- Act on the result of each upload. Go to Get upload results.
- Tune batching, the Batch API, and proxies. Go to Data delivery.
Was this helpful?
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.