Java Native Interface (JNI)

JNI is a programming framework that enables the java code running in a java virtual machine to call native applications specific to operating system and hardware.  JNI enables one to write native methods to handle situations when an application cannot be written entirely in the Java programming language.

Here I am describing how to do programming with JNI with an example. JNI is difficult, as it involves two languages and runtimes. I shall assume that you are familiar with:

  1. Java
  2. C/C++ and the GCC Compiler
  3. (For Windows) Gygwin or MinGW.
  4. (For IDE) Eclipse C/C++ Development Tool (CDT)

Here, I am giving an example with C programming language.

Step 1: Write a Java class that uses native methods

public class HelloJNI {
   static {
      System.loadLibrary("hello"); /* hello.dll (Windows) or (Unixes)*/
   /* A native method that receives nothing...