JNI - “Cannot open include file: 'jni_md.h'”

后端 未结 5 1551
走了就别回头了
走了就别回头了 2020-12-18 17:32

This sample program is meant to call a native method written in C.

Java Code

class HelloWorld {

    private native voi         


        
5条回答
  •  借酒劲吻你
    2020-12-18 18:18

    You should include the following header file in your native code first

    #include 
    

    In my case in UNIX system,

    • This header file jni.h is present at /usr/lib/jvm/java-8-openjdk-amd64/include/

    • Also, jni_md.h is present at /usr/lib/jvm/java-8-openjdk-amd64/include/linux

    You can get the path to above files if you know where the Java installation path redirects you in your system. Get it done by following set of commands.

    whereis java
    /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
    
    ls -l /usr/bin/java
    /usr/bin/java -> /etc/alternatives/java
    
    ls -l /etc/alternatives/java
    /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
    

    where (->) is symbolic link.

    • At last, you get your Java installation path. In my case, it is /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

    Also don't forget to include jni.h & jni_md.h files path while doing their native Compilation.

    Compilation:-

    gcc -I /usr/lib/jvm/java-8-openjdk-amd64/include/ -I /usr/lib/jvm/java-8-openjdk-amd64/include/linux/ -o libHelloWorld.so -shared *nativeSourceCodeFile*.c
    

    where (-I) is Identify the Path.

    • Above command compiles our native code & that's why path of Java Native Interface header file jni.h is provided in reference.
    • jni.h does the explicit import of jni_md.h ie. #include "jni_md.h" & that's why we've provided next reference in our compilation to that jni_md.h file.
    • jni_md.h contains the machine-dependent typedefs for jbyte, jint and jlong.

    Execution:-

    java -Djava.library.path=. HelloWorld
    

    Next,

    JNIEXPORT void JNICALL Java_HelloWorld_print( JNIEnv* env , jobject obj){
        printf("Hello World!\n");
    }
    

    Just see the small changes made and try to implement it.

提交回复
热议问题