Using LexActivator with Java

Adding licensing to your app

After you've added a product for your app in the dashboard, go to the product page of the product you will be adding licensing to. You will need to do two things:
  • Note the product id for the product.
  • Download the Product.dat for the product.
  • Download the example project from Github
Product.dat contains product data that is used by LexActivator. The product id is the identifier of your product that is to be used in the code.

Adding the library to your app

LexActivator wrapper for Java is available on the central maven repository and can be easily installed using maven by adding the following dependency in your POM file:
1
<dependency>
2
<groupId>com.cryptlex.lexactivator</groupId>
3
<artifactId>lexactivator</artifactId>
4
<!-- Make sure that you use the latest version -->
5
<version>3.14.9</version>
6
</dependency>
Copied!
Alternatively, you can also download the JAR file and add it to your project.
LexActivator has a dependency on VS2015 runtime on Windows. On the target machines where you will deploy your app, you can install the VS2015 runtime, if not present, using the link: https://www.microsoft.com/en-in/download/details.aspx?id=48145

Setting product.dat file and product Id

The first thing you need to do is either embed the Product.dat file in your app using SetProductData() function or set the absolute path of the file using SetProductFile() function.
The next thing you need to do is to set the product id of your application in your code using SetProductId() function. It sets the id of the product you will be adding licensing to.
1
LexActivator.SetProductData("PASTE_CONTENT_OF_PRODUCT.DAT_FILE");
2
LexActivator.SetProductId("PASTE_PRODUCT_ID", LA_USER);
Copied!
If your app requires admin (root) privileges to run (e.g. services, daemons etc.), instead of passing LA_USER flag, you need to pass LA_SYSTEM flag.
In case your app doesn't have write access to the disk, you can use LA_IN_MEMORY flag instead, which causes all the data to be stored in the memory. But this would require you to activate the license every time you restart the app.

License activation

To activate the license in your app using the license key, you will use ActivateLicense() LexActivator API function. It invokes the /v3/activations Cryptlex Web API endpoint, verifies the encrypted and digitally signed response to validate the license.
1
int status;
2
try
3
{
4
LexActivator.SetLicenseKey("PASTE_LICENSE_KEY");
5
LexActivator.SetActivationMetadata("key1", "value1");
6
status = LexActivator.ActivateLicense();
7
if (LexActivator.LA_OK == status || LexActivator.LA_EXPIRED == status || LexActivator.LA_SUSPENDED == status)
8
{
9
System.out.println("License activated successfully: " + status);
10
}
11
else
12
{
13
System.out.println("License activation failed: " + status);
14
}
15
}
16
catch (LexActivatorException ex)
17
{
18
System.out.println(ex.getCode() + ": " + ex.getMessage());
19
}
Copied!
The above code should be executed at the time of registration, ideally on a button click.

Verifying license activation

Each time, your app starts, you need to verify whether your license is already activated or not. This verification should occur locally by verifying the cryptographic digital signature of activation. Ideally, it should also asynchronously contact Cryptlex servers to validate and sync the license activation periodically. For this, you need to use IsLicenseGenuine() LexActivator API function.
1
int status;
2
try
3
{
4
LexActivator.SetProductData("PASTE_CONTENT_OF_PRODUCT.DAT_FILE");
5
LexActivator.SetProductId("PASTE_PRODUCT_ID", LexActivator.LA_USER);
6
status = LexActivator.IsLicenseGenuine();
7
if (LexActivator.LA_OK == status)
8
{
9
System.out.println("License is genuinely activated!");
10
}
11
else if (LexActivator.LA_EXPIRED == status)
12
{
13
System.out.println("License is genuinely activated but has expired!");
14
}
15
else if (LexActivator.LA_GRACE_PERIOD_OVER == status)
16
{
17
System.out.println("License is genuinely activated but grace period is over!");
18
}
19
else if (LexActivator.LA_SUSPENDED == status)
20
{
21
System.out.println("License is genuinely activated but has been suspended!");
22
}
23
else
24
{
25
System.out.println("License is not activated: " + status);
26
}
27
} catch (LexActivatorException ex)
28
{
29
System.out.println(ex.getCode() + ": " + ex.getMessage());
30
}
Copied!
The above code should be executed every time user starts the app. After verifying locally, it schedules a periodic server check in a separate thread.

Need more help

In case you need more help with adding LexActivator to your app, we'll be glad to help you make the integration. You can either post your questions on our support forum or can contact us through email.