Portable Makefile for JNI applications

Viewed 389

I have a Java application with a C JNI backend I want to distribute. Distributing the Java part is easy enough -- that's precisely what jars and the JVM are for. Distributing pre-compiled C code is not that easy, and I assume I will have to ask users to build the native library on their own.

At the moment I'm using ant to build the Java code and make to build the C code. The problem is that I have hard-coded the location of jni.h and jni_md.h in my Makefile, which is far from portable. I've searched through as many JNI examples as I could find in the world wide web and all of them had a hard-coded path somewhere. If it were any package other than JNI I could happily use something like pkg-config to get the right include flags, but as far as I'm aware JNI does not provide any such functionality.

Question: how can I write a portable makefile that will find the JNI headers and compile my C native code in any* system?

For reference, here are a few options I've considered:

  • I could use CMake's FindJNI function, but I'd rather not since the C code is not that complex and CMake would be a bit of an overkill (plus an extra dependency for the users).

  • I could hope that the JAVA_HOME environment variable is set, which I don't expect always to be the case, since (at least in Ubuntu) it's not set by default when you install Java.

  • I could use locate or find to manually find the files and stitch the include flags together, but this feels rather hacky.

*I'm mostly interested in Windows, OSX and mainstream Ubuntu versions with any reasonable modern version of Java.

1 Answers
Related