Using JNA with Groovy

Given this embarassingly simple C program which takes a name parameter and a buffer to write the result into, and writes "Hello %s" into this buffer:

Note: ¬†We could have used malloc to generate a return buffer, printed into this and returned it, but then there would have been no way to free this memory, and we would have had a memory-leak…

You can compile it to a shared library with gcc (this for OS X):

Or for Linux:

Then, in a Groovy script, grab the JNA package from maven, define your interface and call the function though the JNA Native class:

And running with:

Prints "Hello Tim".

Can’t really get simpler than that…

@tim_yates

 
Blog comments powered by Disqus