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 |
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.
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.
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:\LEADTOOLS21\Bin\Java\
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:
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.
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.
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
SetLicense();
}
catch(Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
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.
private static void SetLicense() throws IOException {
String licenseFile = "C:\\LEADTOOLS21\\Support\\Common\\License\\LEADTOOLS.LIC";
String developerKey = Files
.readString(Paths.get("C:\\LEADTOOLS21\\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 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
.
This tutorial showed how to add LEADTOOLS references and how to properly set your LEADTOOLS license in a Java application using Visual Studio Code.