Simple JNI Example with Eclipse CDT

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

public class HelloJNI {

    static {
    public native void displayMessage();
    public static void main(String[] args) {
        HelloJNI app = new HelloJNI();

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

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}