Simple JNI Example with Eclipse CDT

1. Create a new Java Project and create a new class called HelloJNI.java and put this code inside:

public class HelloJNI {

    static {
        System.load(“C:/libHelloJNIC++.dll”);
    }
    
    public native void displayMessage();
    
    public static void main(String[] args) {
        HelloJNI app = new HelloJNI();
        app.displayMessage();
    }
}

2. Open a console and enter in your HelloJNI Project source directory and type:

javac HelloJNI.java
javah HelloJNI

This will return a new file called HelloJNI.h

3. Create a new C++ Shared Library Project in Eclipse and put the HelloJNI.h file inside of it.
Now create a new C++ source file called HelloJNI.cpp and put this code inside:

#include <stdio.h>
#include “HelloJNI.h”

JNIEXPORT void JNICALL Java_HelloJNI_displayMessage
  (JNIEnv *env, jobject obj)
{
    printf(“Hello JNI!\n”);
}

4. Now in the Project Properties go to MinGW C++ Linker –> Miscellaneous –> Linker flags and paste -Wl,–kill-at

5. Finally go to Run configurations –> Arguments –> VM arguments and paste -Djava.library.path= and then in Working directory select other and put ${workspace_loc:HelloJNIC++/Debug}