I’m working on a closed source product called Ekwos. It’s a software to manage equestrian competition. The licensing model for this software require to be renew every year. For this reason, I wanted to add a basic licensing module to control who may use the software and for how long.

After some research, I found this article http://blog.afewguyscoding.com/2012/02/licensing-module-java/ providing what I want but I had trouble to adapt the source code for my how use. After couple hours, I refactored the original source code be more flexible and to meet my needs. The module is completely independent from any external module.

The code is release under MIT, BSD and Apache license.

Here is a snippet showing how to use it in your application. This snippet use SWT to display error message. Your application may use whatever you want instead of SWT.

protected boolean checkLicense() {
    // Open the license manager

    LicenseManager licenseManager;
    try {
            URL resource = getClass().getResource("pubkey.der");
            InputStream stream = resource.openStream();
            try {
                    licenseManager = new LicenseManager(stream, null);
            } finally {
                    stream.close();
            }
    } catch (Exception e) {
            MessageUtils.openWarning(null, "The installation is broken. Please re-install.", e);
            return false;
    }

    // Open the license file

    File licenseFile = new File(LICENSE_FILE);
    if(!licenseFile.exists()){
            licenseFile = new File(TRIAL_LICENSE_FILE);
    }
    try {
            App.license = (EkwosLicense) licenseManager
                            .readLicenseFile(licenseFile);
    } catch (InvalidKeyException e) {
            MessageUtils.openWarning(null,
                            "Your licence is invalid or corrupted.", e);
            return false;
    } catch (NoSuchAlgorithmException e) {
            // Should never happen

            MessageUtils.openWarning(null, "The installation is broken. Please re-install.", e);
            return false;
    } catch (SignatureException e) {
            MessageUtils.openInfo(null, "The installation is broken. Please re-install.");
            return false;
    } catch (FileNotFoundException e) {
            MessageUtils.openWarning(null,
                            "Cannot find your licence file.", e);
            return false;
    } catch (IOException e) {
            MessageUtils.openWarning(null, "Cannot read your licence file.",
                            e);
            return false;
    } catch (ClassNotFoundException e) {
            MessageUtils.openWarning(null,
                            "Your licence is corrupted.", e);
            return false;
    }

    // Validate the license data

    String currentVersion = getCurrentVersion();
    try {
            license.validate(new Date(), currentVersion);
    } catch (LicenseExpiredException e) {
            if (license.getExpiration() != null) {
                    String date = DateFormat.getDateInstance().format(
                                    license.getExpiration());
                    MessageUtils.openWarning(null,
                                    "Your licence is expired since:" + date);
            } else {
                    MessageUtils.openWarning(null, "Your licence is invalid.");
            }
            return false;
    } catch (LicenseException e) {
            MessageUtils.openWarning(null, "Your licence is invalid.", e);
            return false;
    }

    return true;
}

You may get the full source code from this location. Jars are also provided.

Edit: The new link to get the files. http://git.patrikdufresne.com/common/license/

Edit Nov 18, 2012: Download link was broken.

Edit Apr 19, 2017: Fix the code formatting. Also translate the error message in english.