JNI - Interoperating with Java
JNI - the Java Native Interface
An application programming interface:
allows Java code from within a JVM to invoke native code (C, C++, assembly)
allows native code to invoke JVM
implementation is JVM specific
Why we want to call native code from Java?
Java API does not support platform-dependent features
Want to use existing library written in native code
need to run time-critical code ina lower-level language
How to use JNI
Declare native method in Java
Implement native method in low-level language
Compile low-level code to create a dynamic library
Add the dynamic library to the JVM library path
During runtime, load dynamic library and invoke method.
How JNI works
Java code requests library load
JVM searches for the named library on its library paths and loads it
Native method is invoked in Java
JVM looks for the method implementation on its loaded libraries
Parameters to the native method are copied and context is switched to run the native implementation outside JVM
Method finishes and results are reflected in the JVM
Execution flow continues as if the method was run by JVM
Last updated
Was this helpful?