JNI or Runtime.exec()?

限于喜欢 提交于 2020-01-01 19:34:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3752082/jni-or-runtime-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!