在Mac下写java 的JNI

原创
2020/04/20 18:44
阅读数 138

开始准备
项目目录:
../demo/java/firstNative
java环境


1.准备native代码
 

public class FirstNative{

  static
    {
        System.setProperty("java.library.path", ".");
        System.loadLibrary("firstnative");
    }
  
  public static native void first();

  public static void main(String[] args){
    first();
  }

}

2生成jni
javah -o firstnative.h -classpath ./ -jni FirstNative

 

3.实现jni

#include "firstnative.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_FirstNative_first(JNIEnv *env,jobject obj)
{
    printf("Hello,First JNI\n");
}

4. 生成

gcc -m64  -I "/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents//Home/include"   -dynamiclib -o libfirstnative.dylib firstnative.c

这里注意会报错:
 

In file included from firstnative.c:1:
In file included from ./firstnative.h:2:
/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents//Home/include/jni.h:45:10: fatal error: 'jni_md.h' file not found
#include "jni_md.h"
         ^~~~~~~~~~

gcc -m64 -I "/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/include/darwin" -I "/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents//Home/include"   -dynamiclib -o libfirstnative.dylib firstnative.c
就行

5 运行

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部