An example project demonstrating how to call a C shared library through JNI in Clojure.
I couldn't find a really complete example of this on the internet, so I decided to put what I made as a proof of concept out for anyone to see.
I wrote up a Dockerfile, so it's as easy as
docker build -t jni-example
docker run --rm -it jni-example
you should see:
Hello from Clojure!
Loading C library from Java!
Hello from C, User!
You'll need:
- A full JDK with headers (e.g. openjdk-8-jdk-devel)
- GCC
- GNU Make
- Leiningen
You should be able to simply run make
once those are in place.
JNI works in a few steps.
- Compile a Java file with
native
methods to a.class
file. - Generate a C header with
javah
. - Use that header in a
.c
file. This is the entrypoint into native code from your JVM code. - Compile your C code into a shared library.
- Compile your JVM code into a JAR.
You use the methods definted in the wrapper Java class to call into native code. When you run the JAR Java will try to find the shared lib, and if it does it'll call the code as needed.
For Clojure, there's just the additional step of Java interop.
- I couldn't get
System.loadLibrary
to work with my shared library. I am unsure if this is by design or not. - Make sure that
JAVA_HOME
is set, so that the C compiler can find the right header.