MATLAB is a programming platform designed specifically for engineers and scientists to analyze and design systems and products that transform our world. It also has the ability to interface with Java classes provided by external parties to perform custom tasks. Today we will look at how to call the IP2Location Java library inside of MATLAB.
First of all, you will need to have MATLAB installed. You’ll also either need a trial or paid license to use it. Download the installer from the below and run it:
After you’ve installed MATLAB, download the latest IP2Location JAR file from the below:
The JAR file should be named like ip2location-java-x.y.z.jar so make sure you get the correct file.
Copy it to a folder e.g., C:\myfolder\ in our case.
Next, you’ll need the IP2Location BIN database file. Download the zipped file containing the BIN file from either of the links below.
For the commercial file, go to https://www.ip2location.com/database/ip2location.
For the free LITE file, go to https://lite.ip2location.com/.
Extract the BIN file into the above folder.
Let’s look at the codes now
Configure the IP2Location JAR class path
Open the MATLAB application and you’ll see something like below:
Inside the Command Window, type the following code to set the IP2Location Java JAR path.
Note that the above is our JAR file version and folder, yours may differ so modify the command accordingly.
We are using the dynamic Java class path style for our demo but for production systems, you may want to use the static path style instead. With the static path, you don’t have to set the class path each time you use MATLAB.
However, we won’t cover the static path style. Please see the below for instructions on setting up the static path.
Initialize the IP2Location Java component
Run the below command to create the IP2Location Java object.
ipl = javaObject('com.ip2location.IP2Location')
You’ll see something like the below which means your IP2Location Java object has been created.
Configure the IP2Location Java component
After creating the IP2Location Java object, you need to configure it with the IP2Location BIN file location to use in geolocation queries.
Run the below to configure the BIN file path.
Querying IP geolocation data
Let’s now query an IP address for the geolocation data. For our example, we’ll use 18.104.22.168 as our IP address.
res = ipl.IPQuery('22.214.171.124')
See the results in the image below.
It is really that easy to incorporate geolocation functionality into your MATLAB codes. All you need is the IP2Location Java component along with the IP2Location BIN database file.