Last active
November 16, 2015 12:03
-
-
Save smamran/9b4f556f0f4fad0dcc9d to your computer and use it in GitHub Desktop.
JNI MinGW Tutorial x64 Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ########## GNU MinGW JNI Tutorial Windows x64 ############ | |
| ##. Copy jni.h jni_md.h to MinGW include Directory | |
| --- JniApp.java ---- | |
| public class JniApp{ | |
| public native int factorial(int number); | |
| public static void main(String[] args){ | |
| JniApp ja = new JniApp(); | |
| int f = ja.factorial(5); | |
| System.out.println("Factorial of 5 is : " + f); | |
| } | |
| /// Add Later if Not Works | |
| static | |
| { | |
| System.loadLibrary("Factorial"); | |
| } | |
| } | |
| ##. javac JniApp.java | |
| ##. javah -jni JniApp | |
| ##. JniApp.h header file contents : | |
| JNIEXPORT jint JNICALL Java_JniApp_factorial | |
| (JNIEnv *, jobject, jint); | |
| change it to JniApp.c by following: | |
| ----- Factorial.c ------- | |
| #include <stdio.h> | |
| #include "jni.h" | |
| #include "JniApp.h" | |
| JNIEXPORT jint JNICALL Java_JniApp_factorial | |
| (JNIEnv *env, jobject obj, jint num) | |
| { | |
| int fact = 1; | |
| while(num > 0){ | |
| fact *= num; | |
| num--; | |
| } | |
| } | |
| ##. gcc -c Factorial.c | |
| ##. gcc -shared -o Factorial.dll Factorial.o | |
| ##. Javac JniApp.java | |
| --- JniApp.java ---- | |
| public class JniApp{ | |
| public native int factorial(int number); | |
| public static void main(String[] args){ | |
| JniApp ja = new JniApp(); | |
| int f = ja.factorial(5); | |
| System.out.println("Factorial of 5 is : " + f); | |
| } | |
| static{ | |
| System.loadLibrary("Factorial"); | |
| } | |
| } | |
| ##. java JniApp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| call javac JniApp.java | |
| call javah -jni JniApp | |
| call gcc -c Factorial.c | |
| call gcc -shared -o Factorial.dll Factorial.o | |
| call Javac JniApp.java | |
| call java JniApp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good work !!