Reader small image

You're reading from  Java Coding Problems - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837633944
Edition2nd Edition
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

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...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837633944

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard