What is Java Native Interface(JNI)

What is Java Native Interface(JNI)?
You may write your application purely in java but there are certain situations where java code might need meet your requirement.
Programmers uses the JNI (Java Native Interface) to write the Java native methods when an application cannot be written purely in Java.

Examples of using JNI (Java Native Interface).

When classes in java library doesn’t support the platform-dependent features needed by the application.
When you want your java code to access the library already written in any other language using JNI.


By using JNI (Java Native Interface), you can make Native methods to -

  • Call the Java methods.
  • Create, and update Java objects like arrays, Integer, String and many more.
  • Catch and throw exceptions in java.
  • Load java classes and obtain class information.


Objectives of JNI (Java Native Interface).

  • With JNI (Java Native Interface) each JVM vendor can support a larger body of native code.
  • Application programmers will be able to write one version of their native code and this version will run on different VMs..


JNI (Java Native Interface) method interface must satisfy the following requirements >

  1. Binary compatibility - Binary compatibility of native method libraries across all JVM implementations on different platforms. Developers must maintain only one version of their native method libraries for a given platform.

  1. Efficiency - We must ensure JVM-independence.

  1. Functionality - The JNI must expose enough JVM internals to allow native methods to accomplish the useful tasks.



Should we program to JNI (Java Native Interface) >
  • If you are looking to implement your own JVM, you should look to implement the JNI (Java Native Interface) .
  • JNI (Java Native Interface) does not impose any overhead or restrictions on your JVM implementation.
  • By using JNI (Java Native Interface) your java code can access the library already written in any other language.
  • JNI (Java Native Interface) does not impact object creation or object cleaning process like garbage collection.


No comments:

Post a Comment