Add licensing to a Java or Kotlin app
At the end, your Java 22+ or Kotlin app activates a licence key and unlocks its features offline.
From your product page in PermitPlane (app.permitplane.com/ops/vendor/), click Download permitplane.json and Create test key.
1. Install
Download permitplane-java-0.1.0.zip and compare its sha256 with manifest.json. It holds the jar and permitplane.dll for Windows x86_64. Put both on your paths and allow native access:
Get-FileHash -Algorithm SHA256 .\permitplane-java-0.1.0.zip
Expand-Archive .\permitplane-java-0.1.0.zip -DestinationPath C:\libs
$pp = "C:\libs\permitplane-java-0.1.0"
javac -cp "$pp\*" Main.java
java --enable-native-access=ALL-UNNAMED `
"-Djava.library.path=$pp" -cp "$pp\*;." Main2. Activate a licence key
Load the config, create the client and activate once:
import com.permitplane.*;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
String config = Files.readString(Path.of("permitplane.json"));
Host host = Host.of(null, null, null, null);
// Loads the saved state, so a restart needs no request.
try (var client = new PermitPlaneClient(config, host)) {
if (client.status().status().equals("free")) {
// paste your test key here; in your real app, ask the user for it
client.activateWithKey(
"PP-7KQ4M-X9T2H-B6WRN-3FJ8D", "Office PC");
}
// ... unlock features here (step 3) ...
}
}
}3. Unlock features in your app
Turn parts of your app on or off based on what the customer's licence includes. The check reads the signed grant and works offline up to your policy's offline days.
if (client.hasFeature("export")) {
enableExport();
} else {
showUpgradeOffer();
}PermitPlaneClient.Limit seats = client.featureValue("seats");
// seats.unlimited(), or seats.value()
// free, trial, paid, online_required, expired,
// recovery_required or seat_unavailable
String status = client.status().status();4. Keep it current
- Call
client.refresh()on start and every hour. - Call
client.persist()now and then and before exit. - Call
client.deactivate()to release the device.
Errors
Every failure throws the unchecked PermitPlaneException.
| Error | What to do |
|---|---|
getCode() is not_a_license_key | The key is mistyped. Ask the user to check it. |
getApiCode() is invalid_license_key | The key is unknown or revoked. Ask for the key from the purchase email. |
getApiCode() is device_limit | Every device slot is taken. Deactivate another device first. |
getApiCode() is license_expired, license_suspended or license_revoked | Show a renew or contact link. |
getCode() is transport | The network failed. Retry later. Unlocked features keep working. |
try {
client.refresh();
} catch (PermitPlaneException e) {
System.err.println(e.getCode() + " " + e.getApiCode());
}Going further
- Unattended and offline activation: request and response files for air-gapped machines.
- Floating licences: concurrent seats with leases.
- Network requirements: what your customer's firewall must allow.
- Reference: Kotlin, platform services, floating seats and offline files.
Reference
Package contents
- The
com.permitplanepackage as a jar for JDK 22 or newer. permitplane.dllfor Windows x86_64.- The licence and the third-party notices.
- Downloads need no sign-in. On Linux or macOS, check a file with
sha256sum <file>. - The client binds the C library with the Foreign Function & Memory API (
java.lang.foreign). - There is no JNI glue and no native build step on your side.
- Without
--enable-native-access=ALL-UNNAMED, JDK 24 and newer print a warning. - You can put the DLL's folder on
PATHinstead ofjava.library.path.
Kotlin
Kotlin uses the same classes. There is no separate Kotlin module.
import com.permitplane.*
import java.io.File
val config = File("permitplane.json").readText()
PermitPlaneClient(config, Host.of(null, null, null, null)).use { c ->
try {
c.activateWithKey("PP-7KQ4M-X9T2H-B6WRN-3FJ8D", "Office PC")
} catch (e: PermitPlaneException) {
println("${e.code} ${e.apiCode} ${e.httpStatus}")
}
if (c.hasFeature("export")) { /* unlock it */ }
println(c.status().status)
}Platform services
Host.of(null, null, null, null) builds four services from the defaults. Pass your own to replace any one.
| Service | Default | Behaviour |
|---|---|---|
StateStore | FileStateStore | Atomic file in the user or machine directory. DPAPI on Windows, mode 0600 elsewhere. It holds a bearer installation token. |
KeyStore | FileKeyStore | One ECDSA P-256 key per namespace. DPAPI on Windows, a 0600 file elsewhere. |
Clock | SystemClock | Monotonic time that includes suspend, plus a boot id. Reports confident = false where it cannot guarantee both. |
Transport | HttpTransport | java.net.http, https only, no redirects, no cookies, 1 MiB response cap. |
- The boot id comes from
NtQuerySystemInformation(90)on Windows. - On Linux it comes from
/proc/sys/kernel/random/boot_id. - An
HttpClient.Builderyou pass in keeps its proxy and TLS settings.
Machine directory rules
- The machine directory is
%ProgramData%\PermitPlane. - The client creates it with a locked-down ACL: SYSTEM, Administrators and the creating user.
- It checks the directory and its parent on every save, load and key creation.
- It refuses them if another account owns them or can read, write or swap them.
- The error says "machine directory ... is not private".
- Activate under the service account that will use the store.
- Installers create
<Vendor>\<Product>for SYSTEM and Administrators only. - The
icaclsrecipe is in Unattended and offline activation.
Exception fields
| Method | Returns |
|---|---|
getStatus(), getCode() | The library status and its short name. |
getApiCode(), getHttpStatus() | The service error code and HTTP status, when the service answered. |
getRetryAfter() | The service's Retry-After as a Duration. |
getLastError() | The raw error JSON. |
getCause() | The exception one of your own services threw. |
Floating seats
checkoutSeat(0)takes an online seat.- Call
renewSeat()at about half ofstatus().lease().ttlSeconds(). checkoutSeat(hours)with 1 to 504 hours borrows a seat for offline use.releaseSeat()returns an online seat early.- No free seat:
getApiCode()isseats_exhaustedandgetRetryAfter()says when to retry. - The status then becomes
seat_unavailable.
Offline files
createOfflineRequest(PermitPlaneClient.OfflineKind.ACTIVATE, "PC")returns a request file for the vendor.importOfflineResponse(text)stores the vendor's answer to it.
Your own services
- Implement
Host, or pass your own services toHost.of. - A service must not call back into the same client. That call fails with
Status.BUSY. - The native boundary catches any
Throwablefrom your services. - It surfaces as a
HOSTerror, or asSIGNERfor a failedKeyStore.sign.
Threading
- You can share one client between threads.
- The library serialises calls on one client.
- Closing the client waits for calls in flight.
- Do not close the client from inside a service.
Good to know
- The client never stores the licence key.
persist()saves the clock high-water mark.deactivate()also clears the local state.