Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disadvantages of using Java Native Interface

I can not get these two disadvantages of using JNI. I want to know more about them:

  • Difficult to debug runtime error in native code

  • Errors in JNI code take down the entire JVM and don't provide any mechanism for graceful recovery

like image 360
Johanna Avatar asked Sep 08 '09 13:09

Johanna


People also ask

What is Java native interface used for?

The Java Native Interface (JNI) enables Java code running in a Java Virtual Machine (JVM) to call and to be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages, such as C, C++ and assembly.


3 Answers

Difficult to debug

  • You need a C / C++ debugger to debug the native code. It's not possible to step through from Java to C/C++ code easily. (though it is possible to debug both simultaneously. I've done it with Eclipse and the CDT plugin, but it's a pain)

Errors in JNI

  • Bad C/C++ code in your native library can / will cause core dumps / segmentation faults that the JVM can't recover from, so your whole app crashes.
like image 181
Glen Avatar answered Sep 27 '22 19:09

Glen


You'll lose one of the advantages of using Java, your application is not platform independent anymore, and many other problems implied from that: maybe you'll be supporting DLLs for Windows and .so files for Linux, each one of them with their own compiler, different debugging tools, different dependencies and maybe different bugs, more complexity for your build process, more code to test, etc.

like image 36
Abel Morelos Avatar answered Sep 27 '22 18:09

Abel Morelos


Difficult to debug runtime error in native code

Have you ever seen a stacktrace in Java? Well they are very user friendly and they tell you most of the times, the line number, class method and what failed. You don't have those on Native code.

Errors in JNI code take down the entire JVM and don't provide any mechanism for graceful recovery

When you run java code, all is run under the control of the JVM, if something goes wrong, the JVM can handle it. You don't have that control using Native code.

like image 34
OscarRyz Avatar answered Sep 27 '22 19:09

OscarRyz