Windows 7的Java原生库,打印



我正在尝试使用gcc为Windows 7构建java本机库。我让它编译和构建。

作为测试,我只是试图在本地代码中做printf()

gcc -o Test.so -shared -O -I/home/fred/jdk1.8.0_65/include -I/home/fred/jdk1.8.0_65/include/linux Test.c
在Test.c:

JNIEXPORT void JNICALL Java_Test_print(JNIEnv *env, jobject jo) {
    printf("Testn");
}

当它运行时,它不会从printf()返回。

如果我删除printf() Java_Test_print()返回。

如果我将printf()替换为while (1) {}按预期挂起

我知道它到达了我的本机代码,但是它没有从printf()返回并且崩溃了JVM。

dll上的ldd显示它正在使用/usr/bin/cygwin1.dll

我怀疑调用约定有问题。即使是这样,我也不知道该怎么做。

是否有人成功开发了Windows的本机库?或者有什么建议。谢谢!

   Step 1:
   Test.c:
   #include <jni.h>
   #include <stdio.h>
   #include <string.h>
    JNIEXPORT void JNICALL Java_Test_print(JNIEnv *env, jobject jo) {
      printf("Testn");
      return;
    }
   Step 2:
    Build .dll file
    gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%include" -I"%JAVA_HOME%includewin32" -shared -o Test.dll Test.c
   Step 3:
    public class Test {
    public native void print();
    static {
        System.loadLibrary("Test");
         }
    public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { 
      Test test = new Test();
              test.print();
     }
  }

参考链接: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

最新更新