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

# OpenFeature provider for Java SDK

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic documents how to get started with the LaunchDarkly OpenFeature provider for the Java SDK.

<Note>
  **Provider quick links**

  LaunchDarkly's OpenFeature providers are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

  <table className="fern-table">
    <thead>
      <tr>
        <th>Resource</th>
        <th>Location</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>OpenFeature Provider API documentation</td>
        <td>[Provider API docs](https://launchdarkly.github.io/openfeature-java-server/com/launchdarkly/openfeature/serverprovider/package-summary.html)</td>
      </tr>

      <tr>
        <td>GitHub repository</td>
        <td>[openfeature-java-server](https://github.com/launchdarkly/openfeature-java-server)</td>
      </tr>

      <tr>
        <td>Sample application</td>
        <td>[Sample OpenFeature Java provider application](https://github.com/launchdarkly/hello-openfeature-java-server)</td>
      </tr>

      <tr>
        <td>Published module</td>
        <td>[Maven](https://mvnrepository.com/artifact/com.launchdarkly/launchdarkly-openfeature-serverprovider)</td>
      </tr>
    </tbody>
  </table>
</Note>

## Get started

The LaunchDarkly OpenFeature provider for the Java SDK is intended for use in multi-user systems such as web servers and application. It is not intended for use in desktop and embedded systems applications.

Follow these instructions to start using the LaunchDarkly OpenFeature provider for the Java SDK in your application.

### Version compatibility

The LaunchDarkly OpenFeature provider for the Java SDK is compatible with the [OpenFeature Java SDK v1.x](https://openfeature.dev/docs/reference/technologies/server/java).

The provider is compatible with Java 11 and above.

## Install the provider and dependencies

First, you need compatible versions of the LaunchDarkly Java SDK and the OpenFeature java-sdk:

<CodeGroup>
  ```text title="Gradle" lines wrap theme={null}
  implementation group: 'com.launchdarkly', name: 'launchdarkly-java-server-sdk', version: '[7.1.0, 8.0.0)'
  implementation 'dev.openfeature:sdk:[1.7.0,2.0.0)'
  ```
</CodeGroup>

Then, add the LaunchDarkly provider for the Java SDK as a dependency in your application:

<CodeGroup>
  ```xml title="XML" lines wrap theme={null}
  <dependency>
    <groupId>com.launchdarkly</groupId>
    <artifactId>launchdarkly-openfeature-serverprovider</artifactId>
    <version>1.2.1</version>
  </dependency>
  ```

  ```text title="Gradle" lines wrap theme={null}
  implementation group: 'com.launchdarkly', name: 'launchdarkly-openfeature-serverprovider', version: '1.2.1'
  ```
</CodeGroup>

Next, import the OpenFeature and LaunchDarkly namespaces in your application code:

<CodeGroup>
  ```java title="LaunchDarkly Java provider" lines wrap theme={null}
  import dev.openfeature.sdk.OpenFeatureAPI;
  import dev.openfeature.sdk.Client;
  import com.launchdarkly.sdk.server.interfaces.LDClientInterface;
  import com.launchdarkly.openfeature.serverprovider.Provider;
  ```
</CodeGroup>

## Initialize the provider

After you install and import the provider, create a single, shared instance of [`Provider`](https://launchdarkly.github.io/openfeature-java-server/com/launchdarkly/openfeature/serverprovider/Provider.html). Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.

Here's how:

<CodeGroup>
  ```java title="Java" lines wrap theme={null}
  public class Main {
      public static void main(String[] args) {
          OpenFeatureAPI.getInstance().setProvider(new Provider("YOUR_SDK_KEY"));

          Client client = OpenFeatureAPI.getInstance().getClient();
      }
  }
  ```
</CodeGroup>

<Note>
  **The Java provider uses an SDK key**

  The LaunchDarkly Java provider uses an SDK key. Keys are specific to each project and environment. They are available on the **SDK keys** page under **Settings**. To learn more about key types, read [Keys](/docs/sdk/concepts/client-side-server-side#keys-and-credentials).
</Note>

## Construct a context

A [context](/docs/home/getting-started/vocabulary#context) is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. The OpenFeature specification calls these [evaluation contexts](https://openfeature.dev/docs/reference/concepts/evaluation-context).

In the LaunchDarkly provider, contexts:

* always have a particular [context kind](/docs/home/getting-started/vocabulary#context-kind). If you do not specify a kind, the provider treats the context as having a "user" kind. To specify a different kind, including a multi-context, you must include a `kind` attribute.
* must have a targeting key. This is optional in the OpenFeature specification, but LaunchDarkly requires a key for evaluation. You can specify this using `targetingKey`, as in the OpenFeature specification, or `key`, which is the typical LaunchDarkly identifier for the targeting key.

Here are examples of a context:

<CodeGroup>
  ```java title="Example user context" lines wrap theme={null}
  EvaluationContext context = new ImmutableContext("example-user-key");
  ```

  ```java title="Example organization context" lines wrap theme={null}
  EvaluationContext context = new ImmutableContext("org-key", new HashMap<String, Value>(){{
    put("kind", new Value("organization"));
  }});
  ```
</CodeGroup>

For additional examples, read [OpenFeature specific considerations](https://github.com/launchdarkly/openfeature-java-server?tab=readme-ov-file#openfeature-specific-considerations) in the provider GitHub repository.

## Evaluate a context

To evaluate feature flags for a context, use the OpenFeature [Evaluation API](https://openfeature.dev/docs/reference/concepts/evaluation-api). For example:

<CodeGroup>
  ```java title="Evaluate a context" lines wrap theme={null}
  boolean value = client.getBooleanValue("example-flag-key", false, context);
  ```
</CodeGroup>

## Access the LaunchDarkly client

You may need access to the `LDClient` from within the [LaunchDarkly Java SDK](/docs/sdk/server-side/java) if you are working on use cases not supported by OpenFeature, such as [migration flags](/docs/sdk/features/migrations) or [sending custom events](/docs/sdk/features/events).

To access the `LDClient`, use `getLdClient()`:

<CodeGroup>
  ```java title="Java" lines wrap theme={null}
  LDClientInterface ldClient = provider.getLdClient();
  ```
</CodeGroup>
