You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -82,9 +83,7 @@ import DotNetFeatureFlagsCode from '../integrate/feature-flags-code/_snippets/fe
82
83
<FlutterFeatureFlagsCode />
83
84
</Tab.Panel>
84
85
<Tab.Panel>
85
-
<blockquoteclass='warning-note'>
86
-
Feature flags are not supported yet in our <ahref="/docs/libraries/java">Java SDK</a>. However, you can integrate them into your project by using the <ahref="/docs/feature-flags/adding-feature-flag-code?tab=api">PostHog API</a>.
Copy file name to clipboardExpand all lines: contents/docs/integrate/_snippets/install-java.mdx
+24-6Lines changed: 24 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
The best way to install the PostHog Java SDK is with a build system like Gradle or Maven. This ensures you can easily upgrade to the latest versions.
2
2
3
-
Look up the latest version in [com.posthog.posthog-server](https://central.sonatype.com/artifact/com.posthog/posthog-server).
3
+
Look up the latest version of [`com.posthog.posthog-server`](https://central.sonatype.com/artifact/com.posthog/posthog-server).
4
4
5
5
#### Gradle
6
6
7
7
All you need to do is add the `posthog-server` module to your `build.gradle`:
8
8
9
9
```gradle_kotlin file=build.gradle
10
10
dependencies {
11
-
implementation 'com.posthog:posthog-server:1.+'
11
+
implementation 'com.posthog:posthog-server:2.+'
12
12
}
13
13
```
14
14
@@ -26,7 +26,7 @@ All you need to do is add the `posthog-server` module to your `pom.xml`:
26
26
27
27
#### Other
28
28
29
-
See [com.posthog.posthog-server](https://central.sonatype.com/artifact/com.posthog/posthog-server) in the Maven Central Repository. Clicking on the latest version shows you options for adding dependencies for other build systems.
29
+
See [`com.posthog.posthog-server`](https://central.sonatype.com/artifact/com.posthog/posthog-server) in the Maven Central Repository. Clicking on the latest version shows you options for adding dependencies for other build systems.
30
30
31
31
### Setup
32
32
@@ -42,13 +42,31 @@ class Sample {
42
42
publicstaticvoidmain(Stringargs[]) {
43
43
PostHogConfig config =PostHogConfig
44
44
.builder(POSTHOG_API_KEY)
45
-
.host(POSTHOG_HOST)
45
+
.host(POSTHOG_HOST)// TIP: host is optional if you use https://us.i.posthog.com
46
46
.build();
47
47
48
-
PostHogInterfacepostHog=PostHog.with(config);
48
+
PostHogInterfaceposthog=PostHog.with(config);
49
49
50
-
postHog.close(); // send the last events in queue
50
+
posthog.flush(); // send any remaining events
51
+
posthog.close(); // shut down the client
51
52
}
52
53
}
53
54
```
54
55
56
+
## Integrating with Spring
57
+
58
+
To see how to integrate the PostHog SDK with Spring, check out this [sample project](https://github.com/PostHog/posthog-android/tree/main/posthog-samples/posthog-spring-sample).
59
+
60
+
## Debug mode
61
+
62
+
If you're not seeing the expected events being captured, the feature flags being evaluated, or the surveys being shown, you can enable debug mode to see what's happening.
63
+
64
+
To see detailed logging, set the debug configuration option to true.
While planned, local evaluation of feature flags is not yet supported in the Java SDK. All feature flag evaluations occur remotely via the PostHog API.
Local evaluation improves performance by fetching flag definitions once and evaluating them locally, rather than making API calls for each flag check.
187
+
188
+
#### Configuration
189
+
190
+
To enable local evaluation, you need:
191
+
192
+
1. A **personal API key** (not your project API key)
193
+
- Generate one at: PostHog → Settings → Account → Personal API Keys
194
+
2. Enable `localEvaluation` in your config
195
+
196
+
```java
197
+
PostHogConfig config =PostHogConfig
198
+
.builder(POSTHOG_API_KEY)
199
+
.host(POSTHOG_HOST)
200
+
.personalApiKey("phx_your_personal_api_key_here")
201
+
.localEvaluation(true)
202
+
.pollIntervalSeconds(30) // Optional: customize the rate which flag definitions are polled (default: 30s)
203
+
.build();
204
+
```
205
+
206
+
> **Note:** Setting the `personalApiKey` automatically enables `localEvaluation` unless explicitly set to `false`.
207
+
208
+
#### Usage with person and group properties
209
+
210
+
For local evaluation to work, you must provide person properties, groups, or group properties that are used in your flag's [release conditions](/docs/feature-flags/creating-feature-flags#release-conditions).
211
+
212
+
Use `PostHogFeatureFlagOptions` to provide these properties:
1.**On initialization:** PostHog fetches all feature flag definitions using your personal API key
282
+
2.**Periodic updates:** Flag definitions are refreshed every 30 seconds (configurable via `pollIntervalSeconds`)
283
+
3.**Local evaluation:** When you call `getFeatureFlag` or `isFeatureEnabled`, PostHog evaluates the flag locally using the cached definitions
284
+
4.**Automatic fallback:** If local evaluation fails (e.g., missing cohort data), PostHog automatically falls back to making an API call
285
+
5.**Fallback caching:** In the event that a flag must be evaluated remotely with an API call, the result is cached according to the [Feature flag caching](#feature-flag-caching) section.
286
+
#### Benefits
287
+
288
+
-**Reduced latency**: No API call needed for most flag evaluations
289
+
-**Lower costs**: Fewer API requests (polling is billed as ~10 flag requests per poll)
290
+
-**Offline support**: Flags continue to work with cached definitions
291
+
292
+
#### Limitations
293
+
294
+
Local evaluation is **not possible** for flags that:
295
+
296
+
1. Have **experience continuity** enabled (set when you check "persist flag across authentication steps")
297
+
2. Are linked to an **early access feature**
298
+
3. Depend on **static cohorts**
299
+
300
+
For these flags, PostHog automatically falls back to remote evaluation via the API.
301
+
302
+
For more details, see our [local evaluation guide](/docs/feature-flags/local-evaluation).
0 commit comments