tags:

views:

639

answers:

6

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

+19  A: 

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.
Glen
+1 for coredump/segfault crashing the JVM. We had some signal processing code (jni) running as a background indexing task in our tomcat instance. A slight misstep in JNI and tomcat was toast.
basszero
+5  A: 

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.

Abel Morelos
even worse: support windows(32,64), linux(32,64,different distros), solaris8,9,10(sparc, sparcv9, 32, 64) ... trying writing CORRECT cross platform native code for ALL of those. Welcome to makefile hell.
basszero
+1  A: 

While this shouldn't be the case in theory, in practice, JNI based code I found to be very brittle -- I've found it to be a maintenance nightmare. Small changes or even just JVM upgrades cause obscure problems. This may have improved with more recent Java versions (I'm harking back to JDK 1.3 or earlier here).

Steve Gilham
A: 

I'm not sure about jni, but if you're using jna, you can set Native.setProtected(true) and it will throw an error instead of crashing the jvm, so that you can catch it in try...catch block. So second disadvantage is not a problem.

tulskiy
I have serious doubts about that. Random bits of C code could, in theory, take down the *machine* if you mess up. At that point, it doesn't matter if the JVM is directly protected against the fault.
jprete
I most cases (usually invalid memory access), jvm will find it and give a chance to recover. I don't think you even have this feature with pure C. And if one writes code that will take down the machine... java is not the one to blame.
tulskiy
A: 

I'm not sure if this helps, but I used a native method to set a static flag in my JNI/C code to turn debug tracing on or off (default is off). Then I used good old printf() calls at the start of each JNI function that only executed if the flag was set. It's crude but it works.

It's also a good idea to provide some kind of version checking between the Java class and its JNI functions. This can be done with a JNI function that gets called from a static class initializer block. If the client has the wrong combination of libraries (i.e., the jarfile got updated but the JNI shared library didn't, or vice versa), you can throw an exception at class load time.

Loadmaster
A: 

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.

OscarRyz
Oscar, native code has core files, (on *nix at least). You can get a lot more info out of a core file than a java stacktrace, if you know what you're doing. Though it takes more effort to work with core files
Glen
@Glen: "...it takes more effort to work with core files..." hence "Difficult to debug runtime error in native code". It was not my intention to claim it was not possible, but to clarify, why is it more difficult.
OscarRyz