views:

71

answers:

2

Is there any good tool to generate java (+JNI support if needed) from a header file so that a C or C++ library can be used as-is. Kind of a reverse of javah. The real functionality would be in the C/C++, the Java would be only a shim on top for certain users.

I'm no expert on JNI but as far as I can see Javah forces you to do this back to front. It forces you to have JNI-isms penetrating unecessarily into the C code unless you write a conversion layer yourself. And writing a conversion layer by hand is basically a waste of time, since all the information is there in the header file to begin with.

+2  A: 

Looks like SWIG works with Java: http://www.swig.org/Doc2.0/Java.html

Maybe this isn't exactly what you're looking for, though, since you do have to add SWIG directives…

Steve M
SWIG is now at version 2. Your link is to 1.3.
Andy Thomas-Cramer
Oops, just went with what google gave me. Fixed.
Steve M
+3  A: 

For C, you can use JNA. You do have to declare function signatures redundantly in Java, but do not have to write any glue code. JNA is very easy to use.

For C or C++, you can use SWIG. SWIG is a little more complex to use, but automatically generates Java wrappers for C++ classes. I'm enjoying it.

Andy Thomas-Cramer
Thanks. After reading through the docs for both a bit. SWIG looks like it is probably the solution I need to use.
idij