Skip to content

Instantly share code, notes, and snippets.

@smamran
Last active November 16, 2015 12:03
Show Gist options
  • Select an option

  • Save smamran/9b4f556f0f4fad0dcc9d to your computer and use it in GitHub Desktop.

Select an option

Save smamran/9b4f556f0f4fad0dcc9d to your computer and use it in GitHub Desktop.
JNI MinGW Tutorial x64 Windows
########## 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
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
@allvee
Copy link
Copy Markdown

allvee commented Nov 16, 2015

good work !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment