JNI - "Cannot open include file: 'jni_md.h'"

A Simple Java Native Interface (JNI) example in Java

  • env:jdk8、macOS 10.15
// Main.java
public class Main {
    public native int intMethod(int i);
    static {
        System.loadLibrary("Main");
    }
    public static void main(String[] args) {
        System.out.println(new Main().intMethod(2));
    }
}
// Main.c:
#include "Main.h"

JNIEXPORT jint JNICALL Java_Main_intMethod(
    JNIEnv *env, jobject obj, jint i)
{
    return i * i;
}

Compile and run:

javac Main.java -h .
gcc -dynamiclib -O3 \
    -I/usr/include \
    -I$JAVA_HOME/include \
    -I$JAVA_HOME/include/darwin \
    Main.c -o libMain.dylib
java -cp . -Djava.library.path=$(pwd) Main

Output:

4

I suspect that jni.h is trying to #include <jni_md.h>, which is then failing because you haven't added its location to your include path.

Try adding both of these entries to your C compiler's include path:

  • C:\Program Files\Java\jdk1.7.0\include
  • C:\Program Files\Java\jdk1.7.0\include\win32

The win32 path might not be necessary, depending on how jni.h is set up.