Monday, July 25, 2011

Java Native Method

JNI: Java native Method is concept to access the native code from Java programming language. Since Java is wrapped with JVM it has no access to native code or API's, this makes Java a platform independent.

The JNI defines a standard naming and calling convention so the Java virtual machine can locate and invoke native methods. In fact, JNI is built into the Java virtual machine so the Java virtual machine can invoke local system calls to perform input and output, graphics, networking, and threading operations on the host operating system. 


these are the following steps involved in writing a sample JNI example.


1. write a java class which will call the native code with in it.
2. generate .h file out of java class this is needed coz, we need to know a common protocol or  
    signature between java and our native code.
3. write native code using the common protocol or signature.


here native code can be in c/c++.


lets start with an example: