Set up Java SDK

Install the package

1<dependency>
2 <groupId>com.launchdarkly</groupId>
3 <artifactId>launchdarkly-java-server-sdk</artifactId>
4 <version>7.0.0</version>
5</dependency>

Initialize the SDK

Java SDK initialization
1import com.launchdarkly.sdk.*;
2import com.launchdarkly.sdk.server.*;
3
4public class Main {
5 public static void main(String[] args) {
6 // A "context" is a data object representing users, devices, organizations, and
7 // other entities. You'll need this later, but you can ignore it for now.
8 final LDContext context = LDContext.builder("EXAMPLE_CONTEXT_KEY")
9 .name("Sandy")
10 .build();
11
12 LDConfig config = new LDConfig.Builder().build();
13
14 // Set your LaunchDarkly SDK key.
15 // This is inlined as example only for onboarding.
16 // Never hardcode your SDK key in production.
17 final LDClient client = new LDClient("YOUR_SDK_KEY", config);
18
19 if (client.isInitialized()) {
20 // For onboarding purposes only we flush events as soon as
21 // possible so we quickly detect your connection.
22 // You don't have to do this in practice because events are automatically flushed.
23 client.flush();
24 System.out.println("SDK successfully initialized!");
25 }
26 }
27}

You can find your server-side SDK key, client-side ID, and mobile key in the Environments section of your Project settings. Use the overflow menu next to the environment name to copy or display the needed key.

The environments list with the overflow menu open.

The Environments list with the overflow menu open.

To learn more, read Initialize the client in the Java SDK reference guide.