Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usefulness of JNI [closed]

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.

like image 695
javamonkey79 Avatar asked Dec 31 '08 06:12

javamonkey79


People also ask

What is the use of JNI?

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++).

What is a JNI wrapper?

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.

What is JNI and JNA?

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.

What is JNI Jobject?

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.


3 Answers

It is very useful. I can see 2 primary reasons to use JNI (there are likely more).

  1. 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.

  2. 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.

like image 91
Evan Teran Avatar answered Nov 08 '22 00:11

Evan Teran


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.

like image 37
Lawrence Dol Avatar answered Nov 08 '22 00:11

Lawrence Dol


I can think of a few uses off the top of my head:

  1. integration with existing low-level (C/C++) APIs that do not have a Java counterpart
  2. integrating with aspects of the system that are not exposed through available Java APIs (direct hardware access, etc.)

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.

like image 36
Marc Novakowski Avatar answered Nov 08 '22 01:11

Marc Novakowski