August 6, 2017

Java Native Interface JNI Interview Questions Answers


What is Native Interface in JAVA?

JNI is the mechanism used to invoke methods written in languages such as C and C++. You can write code using language like C and C++ and declare its native methods and can use the same method in Java using JNI.

JNI exposes JNI functions and pointers that can access Java objects and methods.



Explain Exception Handling in JNI.

JNI exceptions are handled by using the following:

Throw( ) : Throws an existing exception object. Used in native methods to rethrow an exception.

ThrowNew( ) : Creates a new exception object and throws.

ExceptionOccurred( ) : Determines the exception status of throws and not yet cleared.

ExceptionDescribe( ) : Displays the exception and stack trace

ExceptionClear( ) : A pending exception is cleared.

FatalError( ) : Causes a fatal error to raise and does not return.

What is JNI?

JNI stands for Java Native Interface. It is an interface between Java, applications and libraries that are written in other languages. JNI is solely used to interact with “native” code (code written in system level language such as C). For instance, JNI enables the usage of C libraries and C programs to be used in Java programs.

Define JNI functions and pointers.

JNI functions are those which are used by the developers to interact with JVM within a native method. Every JNI function receives a special parameter as its first argument – JNIEnv ; which points to a special JNI data structure of the type JNIEnv_ . One of the elements of JNI data structure is a ‘pointer to an array’ generated by JVM, and each element of this array is again a pointer to a JNI function. A JNI function can be invoked from the native method by referencing these pointers. Every JVM provides a unique implementation of the JNI functions.

Describe exception handling in JNI.

JNI exceptions can be handled by using C++ exception handling. Using throw and catch blocks of C++ and invoking those methods through JNI is one of the solutions. JNI has built in functions for handling exceptions, which is a better choice.

The following methods can be used for handling exceptions:

throw() : An exception object is thrown. It is used in native method for rethrowing an exception

throwNew() : Creates a new object of an exception and throws it.

exceptionDescribe() : The stack trace and the exception will be printed.

exceptionOccurred() : It is used for determining whether an exception is thrown.

exceptionClear() : A pending exception will be cleared.

fatalError() : A fatal error is raised and does not return anything.

Advantages and disadvantages of using JNI.

Advantages:

Developers can take the advantage of Java platform.

Legacy code investments need not be abandoned.

Interoperability issues can be addressed.

Implements two-way interface that allows Java applications to invoke native code and vice versa.

Disadvantages:

It is difficult to debug runtime error in native code.

Security risk is high.

JVM causes down by the errors in JNI code and do not provide mechanism for recovery.

Heavy weight process as all the queries use strings.




Subscribe to get more Posts :