Skip to content

Instantly share code, notes, and snippets.

@dhilst
Created May 6, 2014 16:36
Show Gist options
  • Save dhilst/eb78517ca3ed9db78f46 to your computer and use it in GitHub Desktop.
Save dhilst/eb78517ca3ed9db78f46 to your computer and use it in GitHub Desktop.
JNI Hello World
#include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_sayHello
(JNIEnv *env, jclass class)
{
puts("Hello from C world");
}
class HelloWorld {
static {
System.loadLibrary("HelloWorld");
}
public static native void sayHello();
public static void main(String[] args) {
sayHello();
}
}
JAVA_HOME = /opt/java6
.PHONY: all clean run
all: HelloWorld.class libHelloWorld.so
HelloWorld.class: HelloWorld.java
javac $<
HelloWorld.h: HelloWorld.class
javah HelloWorld
HelloWorld.c: HelloWorld.h
libHelloWorld.so: HelloWorld.c
gcc -fPIC -shared -Wall -o $@ $< -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
run:
LD_LIBRARY_PATH=. java HelloWorld
clean:
rm -rf HelloWorld.class HelloWorld.h libHelloWorld.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment