146. Introducing Java Native Runtime (JNR)
Java Native Runtime (JNR) is another open-source attempt to address JNI’s complexity. It is a serious competitor for JNA, having a more intuitive and powerful API than JNI.
We can add it as a dependency as follows:
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-ffi</artifactId>
<version>2.2.13</version>
</dependency>
Let’s assume that we have the exact same C method (sumTwoInt()
) and the native shared library (math.dll
) from Problem 145.
We start by writing a Java interface containing the 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 {
@IgnoreError
long sumTwoInt(int x, int y);
}
The @IgnoreError
annotation instructs JNR to not save the errno value (https:/...