JNI hacking

For some reason this week I decided it would be handy to learn something about how JNI works. And when I say learn, I mean write something that would work.

The Linux event interface is fairly fresh in my mind, due to working with buffalo’s ruby library for it recently so I decided to re-implement a wrapper for some of that. Only some, because after implementing two native methods plus some extra helper functions, it was a bit too tedious to want to carry on.

I haven’t written any C code since university, and that wasn’t very much, and a long time ago! Doing this exercise made me remember why I don’t particularly want to – it means checking for errors after every function call, and carefully checking the control flow to make sure allocated data structures get freed (and it has been said that the JNI documentation is not too clear on which need freeing). And to add to the pain, JNI code needs lots of extra function calls to access and convert between the Java and native data structures (and those need error checking too).

I’m not even sure if it is really necessary in this case. Linux provides special files in the /proc and /dev filesystems so you can do a lot using file access. I also discovered this Struct library which could help with data conversion without any native code.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*