Light Docs

Integrating in Java

A guide integrating the licensing system in your Java app.

Introduction

This guide will help you integrate the OmegaLicensing system into your Java application.

This guide assumes the usage of Java 11 or higher.

It also assumes the prior setup of the OmegaLicensing web API. That implies that it's available on a public URL and you have a license key to validate.

Code

Add the following code in the initialization of your Java application.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;

public class LicenseValidator {
    public static void validateLicense(String apiUrl, String licenseKey, String productId) {
        try {
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(apiUrl + "/licenses/validate?key=" + licenseKey + "&productd=" + productId))
                    .build();

            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            JSONObject jsonResponse = new JSONObject(response.body());

            if (jsonResponse.getBoolean("valid")) {
                System.out.println("License key is valid.");
            } else {
                System.out.println("Invalid license key. Exiting program.");
                System.exit(1);
            }
        } catch (Exception e) {
            System.err.println("Error validating license: " + e.getMessage());
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        // Replace this API URL with your own api url.
        String API_URL = "http://your-api-url";
        // Replace this license key with the one you want to validate.
        String LICENSE_KEY = "user-submitted-key";
        // Replace this with the product ID of the product you want to validate the license for.
        String PRODUCT_ID = "1";

        validateLicense(API_URL, LICENSE_KEY, PRODUCT_ID);
    }
}

This Java code uses the org.json library for JSON parsing. You'll need to include this dependency in your project.

On this page