How to use a C API in Java? JNI?
I'm building a J2EE project, in which I would like to use an API which is only available in C. I was thinking of using JNI to do so, but after a quick look at this tutorial, it looks that I in order to use JNI, I need to have the source code (.c files) to compile some kind of "JNI library".
In my case, the API only comports the .h with the signature of all the methods, and the already compiled .dll (or .so).
开发者_运维技巧How could I do this?
Thank you!
JNA is a JNI-based library that allows calling normal C functions without needing a JNI-specific wrapper for each one.
Check out JNA. It allows you to use the .DLL directly. All you need to do is write a Java interface with the same functions you need from the .DLL.
Create a small C wrapper for the native library, compile this to a .dll/.so.
For each needed function in the existing C api, create one JNI-compliant C function which simply calls the real API.
JNI offers a pretty low level API for interfacing your Java code with native code. If you are OK with shelling out money, Jinvoke looks like a pretty good alternative which doesn't require you to write any C/C++ code. The plus here is that you get full paid support. If you don't require it, you can go with JNA. Anything but JNI IMO...
精彩评论