> For the complete documentation index, see [llms.txt](https://docs.hackle.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hackle.io/en/development-guide/java-kotlin.md).

# Java/Kotlin

{% hint style="info" %}
Java/Kotlin SDK supports JDK 8 and above.
{% endhint %}

## Add Dependency

[![](https://img.shields.io/maven-central/v/io.hackle/hackle-server-sdk)](https://central.sonatype.com/artifact/io.hackle/hackle-server-sdk)

Add the SDK dependency.

```groovy
repositories {
  mavenCentral()
}

dependencies {
  implementation 'io.hackle:hackle-server-sdk:2.32.0'
}
```

## SDK Initialization

{% hint style="danger" %}
HackleClient must be a singleton.

`HackleClient` manages state internally to assign Variations without blocking the calling thread.\
This requires additional resources.

It should be managed as a single instance throughout the application lifecycle rather than being instantiated for each request.
{% endhint %}

`HackleClient` is a class that provides methods for using the SDK features.

`HackleClient` initialization is required to use the SDK.

### Instantiation

Pass the SDK Key to instantiate `HackleClient`.\
`HackleClient` periodically synchronizes with the Hackle server as a background task to obtain the required information.

* You can find the SDK Key in [SDK Integration Info](https://dashboard.hackle.io/config/sdk-setting) on the Hackle Service Dashboard.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.sdk.HackleClient
import io.hackle.sdk.HackleClients

// Replace YOUR_SERVER_SDK_KEY with the SDK Key.
val hackleClient = Hackle.client(YOUR_SERVER_SDK_KEY)
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.sdk.HackleClient
import io.hackle.sdk.HackleClients

// Replace YOUR_SERVER_SDK_KEY with the SDK Key.
HackleClient hackleClient = HackleClients.create(YOUR_SERVER_SDK_KEY);
```

{% endtab %}
{% endtabs %}

## Shutdown

You must call the `close()` method when the application shuts down. This releases resources in use and sends any remaining events.

{% hint style="danger" %}
If the application exits without calling `close()`, **events may be lost.**
{% endhint %}

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.sdk.HackleClient

hackleClient.close()
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.sdk.HackleClient

hackleClient.close();
```

{% endtab %}
{% endtabs %}

### If you are using Spring Framework

The best approach is to register `HackleClient` as a bean.\
It will be managed as a single instance throughout the application lifecycle, and `close()` will be called automatically when the application shuts down.

### If you are wrapping HackleClient

It is recommended to implement `AutoClosable` in your wrapping class and call `HackleClient`'s `close()` when the wrapper's `close()` is called.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.sdk.HackleClient

class CustomHackleClient(private val hackleClient: HackleClient): AutoCloseable {

    override fun close() {
        hackleClient.close()
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.sdk.HackleClient

public class CustomHackleClient implements AutoCloseable {

    private final HackleClient hackleClient;

    public CustomHackleClient(HackleClient hackleClient) {
        this.hackleClient = hackleClient;
    }

    @Override
    public void close() throws Exception {
        hackleClient.close();
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hackle.io/en/development-guide/java-kotlin.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
