Skip to content

Commit b78784b

Browse files
author
Juraj Veverka
committed
created simple JNI demo
1 parent 8e2f059 commit b78784b

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

simple-jni-demo/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Display.h
2+
Display.class
3+
libDisplay.so
4+

simple-jni-demo/Display.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <jni.h> // JNI header provided by JDK
2+
#include <stdio.h> // C Standard IO Header
3+
#include "Display.h" // Generated
4+
5+
// Implementation of the native method sayHello()
6+
JNIEXPORT void JNICALL Java_Display_setPixel(JNIEnv *env, jobject thisObj, jint x, jint y, jint r, jint g, jint b) {
7+
printf("setting pixel\n");
8+
return;
9+
}
10+

simple-jni-demo/Display.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Display {
2+
3+
static {
4+
System.out.println("Loading Library...");
5+
System.loadLibrary("Display");
6+
System.out.println("Library loaded.");
7+
}
8+
9+
public native void setPixel(int x, int y, int r, int g, int b);
10+
11+
public static void main(String[] args) {
12+
Display display = new Display();
13+
display.setPixel(0,0, 0, 255, 0);
14+
System.out.println("done.");
15+
}
16+
17+
}
18+

simple-jni-demo/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Very simple Java JNI demo
2+
3+
## create JNI bindings
4+
`javac -h . Display.java`
5+
6+
## compile c
7+
`gcc -I"/opt/java11/include/" -I"/opt/java11/include/linux" -lc -shared -o libDisplay.so Display.c`
8+
9+
## compile java
10+
`javac Display.java`
11+
12+
## run test
13+
`java -Djava.library.path=. Display`
14+
15+
## clean
16+
`rm Display.h libDisplay.so Display.class`
17+

0 commit comments

Comments
 (0)