Add References and Set a License with Visual Studio Code - Java

This tutorial shows how to create a project, add LEADTOOLS references, and set your LEADTOOLS license, in a Java application using Visual Studio Code and the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to add LEADTOOLS references and set a LEADTOOLS license in a Java application using Visual Studio Code
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Java Application
IDE Visual Studio Code
Development License Download LEADTOOLS

Required Knowledge

Before any functionality from the SDK can be leveraged, a valid license will have to be set. For instructions on how to obtain a runtime license, refer to Obtaining a License.

Creating and running Java projects in Visual Studio Code requires installing the Coding Pack for Java. The link below contains the Coding Pack for Java Windows and macOS installer.

Create the Project

Open an instance of Visual Studio Code and select the Explorer view on the left side. To create a new blank Java project select Create Java Project. Select No build tools and name your project. This will create the bare bones of your Java application.

Screenshot of where to click to create the Java project.

Add LEADTOOLS References

In the Explorer view, find the Java Projects tab. Select the newly created project, and then click the + sign on Referenced Libraries. For this tutorial, we will only need the Leadtools.jar which can be found here: C:\LEADTOOLS22\Bin\Java\

Screenshot of where to click to add the LEADTOOLS references.

Note

If you do not see the Java Projects tab, ensure that a .java file is open in your VS Window. If you still do not see the tab, right-click on the .java file and select Reveal in Java Project Explorer

At the top of the App.java file, include the following import statements:

Java
import java.io.IOException; 
import java.nio.file.*; 
 
import leadtools.*; 

Inside the App class add a new method named run(String[] args), optionally passing in the program arguments that can be used later. Call this method inside the main() method as shown below.

Java
public static void main(String[] args) throws Exception { 
    new App().run(args); 
} 

Add the code below to the new run() method to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported.

Java
private void run(String[] args) { 
    try { 
        Platform.setLibPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64"); 
        Platform.loadLibrary(LTLibrary.LEADTOOLS); 
 
        SetLicense(); 
    } 
    catch(Exception ex) { 
		System.err.println(ex.getMessage()); 
		ex.printStackTrace(); 
    } 
} 

Add the Set License Code

Create a new method inside the App class named SetLicense(). Call this method at the bottom of the run() method, as shown above. Add the code below to the SetLicense() method to properly set your LEADTOOLS runtime license.

Java
private static void SetLicense() throws IOException { 
    String licenseFile = "C:\\LEADTOOLS22\\Support\\Common\\License\\LEADTOOLS.LIC"; 
    String developerKey = Files.readString(Paths.get("C:\\LEADTOOLS22\\Support\\Common\\License\\LEADTOOLS.LIC.KEY")); 
    RasterSupport.setLicense(licenseFile, developerKey); 
    if (RasterSupport.getKernelExpired()) 
        System.out.println("License file invalid or expired."); 
    else 
        System.out.println("License set successfully."); 
} 

Run the Project

Run the project by pressing F5 or Run -> Start Debugging. If the steps were followed correctly, a message should appear in the the TERMINAL window stating License set successfully.

Wrap-up

This tutorial showed how to add LEADTOOLS references and how to properly set your LEADTOOLS license in a Java application using Visual Studio Code.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.