Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite C code in Java or use JNI?

I'm currently developing on a project written in Java. We have a bunch of algorithms written in C/C++ (at least a couple hundred) that need to be incorporated in our project. Our two options are to use JNI to call this code, or to rewrite all the algorithms in Java.

I am aware of the consequences of using JNI, it can open up a whole new set of problems, which is why rewriting all the code in Java is even being considered. But the thought of rewriting it seems...wrong. The algorithms have been tested and work as far as I know, they're just in the wrong language.

In this situation, would JNI make this task easy? Or would it cause more headache than rewriting the code in Java would?


EDIT #1: Related Question - Usefulness of JNI


EDIT #2: FYI - Our Java project is not meant to be portable in any way. That might eliminate one of the downsides of JNI in that it supposedly reduces portability.

like image 463
Ryan Thames Avatar asked Mar 04 '09 16:03

Ryan Thames


People also ask

Is JNI faster than Java?

Java Native Access (JNA) Still, it has some of the disadvantages of being JNI-based and is slightly slower than JNI in many cases.

Why do we use JNI?

JNI provides functions for accessing the contents of array objects. While arrays of objects must be accessed one entry at a time, arrays of primitives can be read and written directly as if they were declared in C.

What is JNA vs JNI?

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.

Does JNI create new process?

The advantage of using JNI is that both the calling program and the called program run in the same process (job) while the other methods start a new process (job). This makes JNI calls faster at startup time and less resource-intensive.


2 Answers

The simple answer is, if the code is going to be called a lot AND performance matters then convert it over to Java.

The more complex answers are:

  • if the library easily wrapped in JNI then go with JNI
  • if the tests you have for the C/C++ code easily converted over to Java then go for the port

I would do the following:

  • take one of the algorithms and wrap it in JNI
  • take the same algorithm and convert it to Java
  • see which is more of a pain to do
  • if speed matters then profile both versions and see which of them is acceptable.
like image 122
TofuBeer Avatar answered Sep 28 '22 08:09

TofuBeer


I think the answer lies in the amount of coupling there would be between the calling java code and the invoked C/C++ code and in the level of effort the rewrite would entail. If your C code takes a few integers, does some hairy calculation, and returns another int. Use JNI. If there's a lot of complex back and forth, but the algorithms are reasonably simple, rewrite 'em. The fault line is the JNI connection. If that's going to be complex, you may end up writing more JNI interface code than you would algorithm code for a rewrite.

like image 36
sblundy Avatar answered Sep 28 '22 10:09

sblundy