JNI or Runtime.exec()?

I need to call a RPC client which is implemented in C from a Java class.

The interaction is one way only(i.e) Java has to invoke specific functions in C, while C need not return anything to the calling Java code.

Can someone explain me the pros & cons in using either of the types (JNI/Runtime.exec)?? and which is the best option for my case?

Answers


Runtime.exec() will launch a separate process for each call. Your Java caller needs to consume the output of each process.

JNI would require a native, dynamically linked library. Depending on your operating system, you may need to explicitly export functions. You would define a Java class with "native" methods, generate a C header/stub file with javah, and implement the native methods by calling your C client functions.

Runtime.exec() probably consumes the most resources. I personally would use an in-process call to native code.

Instead of JNI, consider using JNA, which makes it easy to call C functions from Java without an ad hoc native glue layer. Your C functions would need to be in a native, dynamically linked library. In Java, you declare their signatures, load the library, and call the functions.

For Windows DLLs, be aware that you need to export functions for them to be available from outside the DLL.


Need Your Help

Applying intersect for each row in different matrices in MATLAB

matlab vectorization

I have two big matrices and I would like to perform an intersect operation between each row of these matrices. I would get something like that:

Capistrano 3 Permission Denied

ruby-on-rails ruby capistrano capistrano3

I'm converting an app from capistrano version 2 to version 3 and now i'm getting this error: