145. Introducing Java Native Access (JNA)
Java Native Access (JNA) is a brave open-source attempt to address JNI complexity via a more intuitive and easy-to-use API. Being a third-party library, JNA must be added as a dependency in our project:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.8.0</version>
</dependency>
Next, let’s try to call the same sumTwoInt()
method from Problem 144. This function is defined in a C native shared library named math.dll
and stored in our project in the jna/cpp
folder.
We start by writing a Java interface that extends JNA’s Library
interface. This interface contains declarations of methods and types that we plan to call from Java and are defined in native code. We write the SimpleMath
interface containing the sumTwoInt()
declaration as follows:
public interface SimpleMath extends Library {
long sumTwoInt...