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

# Java

> Getting started guide for the Java SDK

## Introduction

The easiest way to set up licensing in your Java application is by using our Java SDK. It's available on the link below:

* [https://github.com/Cryptolens/cryptolens-java](https://github.com/Cryptolens/cryptolens-java)

## Getting started

### Adding the library

There are two pre-compiled jar files: `cryptolens.jar` and `cryptolens-android.jar`. If your application is cross platform or if you would like to have as few dependencies as possible (e.g., without `slf4j`), we recommend to use `cryptolens-android.jar` instead.

If you choose to use `cryptolens-android.jar`, `GetMachineCode` and `IsOnRightMachine` need to be called with the version parameter set to 2. For example, `Helpers.GetMachineCode(2)` or `Helpers.IsOnRightMachine(license, 2)`. If your application will run on an Android device, we recommend to use a different way to obtain the machine code, which is described [<u>here</u>](https://help.cryptolens.io/getting-started/ios-android).

### **Key verification**

The following example is similar to what is covered in the [<u>key verification tutorial</u>](/examples/key-verification).

Assuming you have referenced the `cryptolens.jar` file, the code below should generate successful result. A working project with the code below can be found in the [<u>example-app</u>](https://github.com/Cryptolens/cryptolens-java/tree/master/example-app) folder.

```java theme={null}
import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public class Main {

    public static void main(String[] args) {
        String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";

        LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode(2)));

        if (license == null || !Helpers.IsOnRightMachine(license, 2)) {
            System.out.println("The license does not work.");
        } else {
            System.out.println("The license is valid!");
            System.out.println("It will expire: " + license.Expires);
        }
    }
}
```

In order to adjust this code to your products, several parameters needs to be changed, which we outline below:

* `RSAPubKey` - the RSA Public key, which can be found on [<u>this</u>](https://app.cryptolens.io/docs/api/v3/QuickStart#api-keys) page.
* `auth` - the access token (can be found [<u>here</u>](https://app.cryptolens.io/docs/api/v3/QuickStart#api-keys), in *API Keys* section).
* `product_id` - the id of the product can be found on the product page (in the example above, it's 3646).
* `key` - the license key to be verified (above it's MPDWY-PQAOW-FKSCH-SGAAU).
* `machine_code` - the unique id of the device, which may require root access. Note, this value is not the same as the one generated by the .NET client.

<Note>
  **Note**: The code above assumes that node-locking is enabled. By default, license keys created with Maximum Number of Machines set to zero, which deactivates node-locking. As a result, machines will not be registered and the call to Helpers.IsOnRightMachine(license) will return False. You can read more about this behaviour [<u>here</u>](https://help.cryptolens.io/faq/index#maximum-number-of-machines). For testing purposes, please feel free to remove Helpers.IsOnRightMachine(license) from the if statement.
</Note>

### **Offline activation (saving/loading licenses)**

Assuming the license key verification was successful, we can save the result in a file so that we can use it instead of contacting Cryptolens.

```java theme={null}
String licenseString = license.SaveAsString();
```

When loading it back, we can use the code below:

```java theme={null}
LicenseKey newLicense = LicenseKey.LoadFromString(RSAPubKey, licenseString);
```

If you want to make sure that the license file is not too old, you can specify the maximum number of days as shown below (after 30 days, this method will return null).

```java theme={null}
LicenseKey newLicense = LicenseKey.LoadFromString(RSAPubKey, licenseString, 30);
```

<Note>
  **Note:** `LicenseKey.LoadFromString` does not check the *ProductId*. In case you have multiple products, we recommend that you check that the *ProductId* corresponds to the product where the user tries to use the license file.
</Note>

### **Floating licenses**

[<u>Floating licenses</u>](https://help.cryptolens.io/licensing-models/floating) can be enabled by passing a floatingTimeInterval to the `ActivateModel`. Optionally, you can also allow customers to exceed the bound by specifying the maxOverdraft.

The code below has a floatingTimeInterval of 300 seconds and maxOverdraft set to 1. To support floating licenses with overdraft, the call to `Helpers.IsOnRightMachine(license, true, true)` needs two boolean flags to be set to true.

```java theme={null}
import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public static void main(String args[]) {
    String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
    String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";

    LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(2), 300, 1));
    if (license == null || !Helpers.IsOnRightMachine(license, 2, true, true)) {
        System.out.println("The license does not work.");
    } else {
        System.out.println("The license is valid!");
        System.out.println("It will expire: " + license.Expires);
    }
}
```

### **Deactivation**

In order to deactivate a license, we can call the `Key.Deactivate` method, as shown below. Note, we need an access token with Deactivate permission, so the ones used in earlier examples cannot be used (unless they have this permission).

```java theme={null}
import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public static void main(String args[]) {
    String auth = "";

    boolean result = Key.Deactivate(auth, new DeactivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(2)));
    if (result == true) {
        System.out.println("Deactivation successful.");
    } else {
        System.out.println("Deactivation failed.");
    }
}
```

The call above is useful when [<u>node-locking</u>](https://help.cryptolens.io/licensing-models/node-locked) is used. If it's a floating license, deactivation is not necessary since licenses will be deactivated automatically after a certain period of time. However, to force a deactivation earlier, you can use similar code as above with addition of a boolean flag.

```java theme={null}
import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public static void main(String args[]) {
    String auth = "";

    boolean result = Key.Deactivate(auth, new DeactivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(2), true));
    if (result == true) {
        System.out.println("Deactivation successful.");
    } else {
        System.out.println("Deactivation failed.");
    }
}
```

### **Calling through the license server**

If you would like to re-route the requests through our [<u>license-server</u>](https://github.com/cryptolens/license-server) that is installed on the client site, you can specify its url using `LicenseServerUrl` parameter. All API models expose this parameter.

For example, let's suppose that your client runs the license server on `http://10.1.1.6:8080` and you want to call `Key.GetKey()`. In this case, we first define all parameters for the request and then modify the license server url:

```java theme={null}
GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
model.LicenseServerUrl = "http://10.1.1.6:8080";
```

We do this because there is currently no overload method that accepts `LicenseServerUrl` parameter.

The entire code is shown below:

```java theme={null}
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTn+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==<Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

String auth = APIKey.get("getkeyactivate");

APIError error = new APIError();

GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
model.LicenseServerUrl = "http://10.1.1.6:8080";

LicenseKey license = Key.GetKey(auth, RSAPubKey, model , error);

if (license == null) {
    System.out.println("The license does not work.");
    System.out.println("Error: " + error.message);
} else {
    System.out.println("The license is valid!");
    System.out.println("It will expire: " + license.Expires);
}
```
