I've been a java developer for a couple years and have heard that you can do some pretty useful and powerful things with JNI. I don't know if I just haven't needed to use it or if it isn't terribly relevant to me; but I have not had to touch it at all.
I am wondering what the usefulness of this aspect of Java is. Examples would be great.
JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).
JNIWrapper allows Java applications to interoperate with native applications and libraries written in different programming languages, such as C/C++, Pascal, ASM, etc. Cross-platform Java integration solutions which can be built on top of existing cross-platform native libraries.
Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.
jobject thiz means the this in java class. Sometimes if you create a static native method like this. void Java_MyClass_method1 (JNIEnv *, jclass); jclass means the class itself. Follow this answer to receive notifications.
It is very useful. I can see 2 primary reasons to use JNI (there are likely more).
Performance. If you have a piece of code in which the Java Runtime for whatever reason can't cut it performance wise. You can implement that function in native code and call it from Java. This allows you to hand tune the implementation if you really need to. This is likely the least common reason though, Java usually performs just fine.
Access to OS Specific APIs. This one is a biggie. I've had a case where I needed to do something in Java but also needed access to something that Java simply could not provide. In my case it was UNIX domain sockets. Since (as you can tell by the name) they are UNIX specific, there is no standard Java way to use them. So I made a class that acted like a wrapper around them in C and accessed it with JNI. viola, problem solved.
I have written an extensive JNI layer for the iSeries to access DB2, user queues, data queues and a few other OS/400 specifics. Much of our system on the iSeries would have been impossible without JNI. So, yes, JNI has it's place, and when you need it, you really need it.
It's relatively difficult (next to pure Java), but powerful and not horrible.
Also, whenever I am looking at some JNI code, I also consider using JNA.
I can think of a few uses off the top of my head:
Some might say it can be useful for creating highly-optimized sections of code however with modern JVMs you're getting pretty fast and the complications of using JNI would probably outweigh any performance benefit you might see.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With