Kasinaat 007
Kasinaat 007

Reputation: 144

What is the real world application of Java Native Interface?

I have successfully executed a simple program using JNI and I know what JNI is But I wonder where it is used in real time and why we need JNI while we can do anything in java.

Upvotes: 3

Views: 2079

Answers (4)

awatan
awatan

Reputation: 1210

Although I haven't used JNI lately but about 6 or 7 years ago, I was working on a Java application where we were implementing cryptography. I implemented some of the cryptographic algorithms in C to improve their performance and used JNI as a wrapper.

Upvotes: 2

Javadee
Javadee

Reputation: 169

I will add one real life use case here. Our software application connects to different vendors to get data from them. For this we use vendor supplied api's. 2 of our vendors only have C/C++ API's and since our application is in Java we use JNI to communicate with these API's.

Although JNI is quite brittle to use but if it did not exist it would mean we would have to rewrite parts of our application in C++ which would be tens of thousands of lines of code + the maintenance cost of maintaining code bases in 2 different languages.

One possible way to avoid JNI in our situation is to have messaging middleware that could be used for communications between Java and C++ process's but then we have latency issues + configuration and maintenance of another complex piece of software to deal with.

Upvotes: 3

Daniele
Daniele

Reputation: 2837

An important application of JNI is to wrap existing libraries; the alternative would be to port huge projects (with the cost associated with writing the new code and testing).

See in example QuantLib, https://www.quantlib.org . They port the whole library to Java with SWIG, which in turn uses JNI.

Upvotes: 4

Stephen C
Stephen C

Reputation: 719561

... while we can do anything in java

That's the flaw in your thinking. There some things that you cannot do in Java. There are other things that you cannot do efficiently in Java.

Examples include:

  • interacting with OS functions in ways that the Java APIs don't support,
  • calling libraries implemented in other programming languages, and
  • interacting with memory-mapped devices.

Upvotes: 7

Related Questions