144. Introducing Java Native Interface (JNI)
Java Native Interface (JNI) was the first Java API meant to act as a bridge between JVM bytecode and native code written in another programming language (typically C/C++).
Let’s suppose that we plan to call via JNI a C function on a Windows 10, 64-bit machine.
For instance, let’s consider that we have a C function for summing two integers called sumTwoInt(int x, int y)
. This function is defined in a C shared library named math.dll
. Calling such functions from Java (generally speaking, functions implemented by native shared libraries) starts with loading the proper shared native library via System.loadLibrary(String library)
. Next, we declare the C function in Java via the native
keyword. Finally, we call it with the following code:
package modern.challenge;
public class Main {
static {
System.loadLibrary("math");
}
private native long sumTwoInt(int x, int y);
public static void main(String...