# Java SDK Configuration and Usage

Within this guide, we will meticulously navigate the sequential procedure of enabling your LicenseSpring license by employing the LicenseSpring Java SDK.

As you progress through this tutorial, you will acquire the skills to initiate, deactivate, and verify licenses directly from your Java program.

Functions and methods used throughout this tutorial throw exceptions to help surface any potential problems.

For more information about these, see [**Java Exception Handling**](https://docs.licensespring.com/sdks/java-sdk/java-exception-handling).

### Configuration

To initialize the SDK you will need to fill in your application name and version, LicenseSpring API key, shared key and product code.

Both the API key and shared key can be found on your LicenseSpring account under Settings->Keys:

![API and Shared Key Location](https://content.gitbook.com/content/gLzurdfXUuKr9IziZWLz/blobs/aJ4qA3dXmrXGfjSva9l6/GyXxaw6jWl3gCgn%20lKMFn_image.png)

Your product code is located in your product list under "Configure Products":

![Product Code Location](https://content.gitbook.com/content/gLzurdfXUuKr9IziZWLz/blobs/UV86BfNoQcdW0peok3ZC/6uCNXLQ1QYkYrMk0bXDLd_image.png)

Please keep in mind that the product code field is case-sensitive.

When you have the necessary values, create the configuration.

{% code title="LicenseSpringConfiguration.java" %}

```java
LicenseSpringConfiguration configuration = LicenseSpringConfiguration.builder()
                .apiKey("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
                .productCode("javatutorial")
                .sharedKey("XXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .appName("NAME")
                .appVersion("VERSION")
                .build();
```

{% endcode %}

Now you can initialize `LicenseManager` using this configuration object:

{% code title="Initialize LicenseManager" %}

```java
LicenseManager licenseManager = LicenseManager.getInstance();

try {
  licenseManager.initialize(configuration);
} catch (LicenseSpringException e) {
  System.out.println(e.getCause().getMessage());
}
```

{% endcode %}

### License Activation

The license keys are located in "Licenses" section on the vendor platform:

![License Key Location](https://content.gitbook.com/content/gLzurdfXUuKr9IziZWLz/blobs/otyvATZwcPySSxK4cPyB/jTyebUmXTL07JEg931EtD_image.png)

To implement user-based licensing, see [**User Based Licensing**](https://docs.licensespring.com/sdks/tutorials/licensing-scenarios/user-based-licensing).

To activate a license, create an `ActivationLicense` using the given key and call the activation method.

{% code title="Activate a License" %}

```java
ActivationLicense key = ActivationLicense.fromKey("XXXX-XXXX-XXXX-XXXX");
License currentLicense = licenseManager.activateLicense(key);
currentLicense = licenseManager.checkLicense(currentLicense);
```

{% endcode %}

{% hint style="info" %}
Note: We immediately check our license after activating because `activateLicense` only activates the license on the LicenseSpring servers. The license check either creates or updates the local license file, depending on if it existed prior.
{% endhint %}

### License Deactivation

Deactivating a license unbinds the device from the license and decrements the number of total activations.

Depending on the license transfer policy, this usually means that the license is freed up to be activated onto a new device.

Deactivation is done using the following method:

{% code title="Deactivate a License" %}

```java
if (licenseManager.deactivateLicense(LicenseIdentity.fromKey("XXXX-XXXX-XXXX-XXXX"))) {
  System.out.println("License deactivated successfully.");
}
```

{% endcode %}

### Local License

The local license is a copy of the license information that is saved on the end user's local computer. Users who have not activated the license on their end before will not have a local license. By default, the local license is stored: %USERPROFILE%\AppData\Local\LicenseSpring"Product Code"\License.key.

See [**Local License File**](https://docs.licensespring.com/sdks/tutorials/best-practices/local-license-file) for more information.

If a device already has a license, and you try to activate another license on that device for the same product, the newly activated license will override the existing license, deleting it from the device. It is the developer's responsibility to check if a license exists on the device before a new activation:

{% code title="Check for existing local license" %}

```java
License license = licenseManager.getCurrent();

if (license == null ) {
  // activate a new license
}
```

{% endcode %}

### License Check

It is recommended to perform a local license check at application start to confirm that the local license file belongs to the current device and has not been transferred.

{% code title="Local license check" %}

```java
license.localCheck();
```

{% endcode %}

{% hint style="info" %}
Note: It is also useful to check whether the local license file has been tampered with and whether the local license is still valid.
{% endhint %}

Online license check refreshes the local license with the data from LicenseSpring backend:

{% code title="Online license check" %}

```java
license = licenseManager.checkLicense(license);
```

{% endcode %}

### Full Code Sample

Below is a code sample that initializes the SDK, retrieves a local license or activates a new one, and performs the necessary checks.

{% code title="Full Example" %}

```java
LicenseSpringConfiguration configuration = LicenseSpringConfiguration.builder()
                .apiKey("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
                .productCode("javatutorial")
                .sharedKey("XXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .appName("NAME")
                .appVersion("VERSION")
                .build();
                
LicenseManager licenseManager = LicenseManager.getInstance();

try
  {
    licenseManager.initialize(configuration); 

    License currentLicense = licenseManager.getCurrent();
    
    if (currentLicense == null) {
      ActivationLicense key = ActivationLicense.fromKey("XXXX-XXXX-XXXX-XXXX");
      currentLicense = licenseManager.activateLicense(key);
    }
    
    currentLicense = licenseManager.checkLicense(currentLicense);
    currentLicense.localCheck(); // additional check, not needed if you're doing online check, but good for offline mode
  } catch (LicenseSpringException e) {
    System.out.println(e.getCause().getMessage());
  }
```

{% endcode %}
