A hello-world program using Java Native Interface (JNI). There’s nothing particularly difficult; the option to generate the header and the command to compile the share library might not be obvious though.

1
2
3
4
5
6
7
8
9
class JNIHello {
static native void hello();

static { System.loadLibrary("JNIHello"); }

public static void main(String[] args) {
hello();
}
}
1
2
3
4
5
6
#include <stdio.h>
#include "JNIHello.h" // generated

JNIEXPORT void JNICALL Java_JNIHello_hello(JNIEnv *, jclass) {
puts("Hello world");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
default : run-jni-hello

# using oneshell so that we can define my_jdk_home variable across lines
.ONESHELL:
SHELL = /usr/bin/zsh
.SHELLFLAGS = -i -ec

.PHONY: run-jni-hello
run-jni-hello
my_jdk_home=...
# echo $${my_jdk_home}
javac -h . JNIHello.java
clang++ -shared -fPIC -o libJNIHello.so \
JNIHello.cpp \
-I$${my_jdk_home}/include/ \
-I$${my_jdk_home}/include/linux
$${my_jdk_home}/bin/java -Djava.library.path=. JNIHello.java