Java JNI C程序适用于Mingw32而不适用于Cygwin64

时间:2015-08-03 09:11:54

标签: java c java-native-interface cygwin mingw32

下面给出的Java JNI程序适用于环境(1)。但是,如果我将环境改为(2)它会给出错误。任何帮助,以解决此错误。

Java代码:

package avajjni;
public class AvajJNI {
    static{
    //Environment (1)
    //System.load("D:\\cpro\\c_jni_library\\dist\\Debug\\MinGW_32-Windows\\libc_jni_library.dll");

    //Environment (2)
    System.load("D:/cpro/c_jni_library/dist/Debug/Cygwin_64-Windows/libc_jni_library.dll");
    }
    public native void fnDisplayData();
    public static void main(String[] args) {
        AvajJNI obj = new AvajJNI();
        obj.fnDisplayData();
    }
}

C代码(.c)

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

JNIEXPORT void JNICALL Java_avajjni_AvajJNI_fnDisplayData
  (JNIEnv *env, jobject obj){
    printf("JNI Method Called\n");
}

环境(1)工作正常:
    操作系统:Windows 8.1 64位
    Java jdk1.8.0_51(32位)
    Mingw 32位
    C编译器选项:-shared -m32 -Wl, - add-stdcall-alias
    Netbeans使用32位Java运行

环境(2),它给出了错误:
    操作系统:Windows 8.1 64位
    Java jdk1.8.0_51(64位)
    Cygwin 64位
    没有编译器选项
    &#34; C:\ cygwin64 \ BIN&#34;添加到路径环境变量
    Netbeans使用64位Java

运行

错误:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180126947, pid=7528, tid=20276
#
# JRE version: Java(TM) SE Runtime Environment (8.0_51-b16) (build 1.8.0_51-b16)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [cygwin1.dll+0xe6947]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

如果需要任何其他信息,请与我们联系

1 个答案:

答案 0 :(得分:0)

我重现了这个问题,然后展示了如何解决它:

mdorey@VXD0141 ~/tmp
$ cat avajjni/AvajJNI.java
package avajjni;
public class AvajJNI {
    static{
    //Environment (1)
    //System.load("D:\\cpro\\c_jni_library\\dist\\Debug\\MinGW_32-Windows\\libc_jni_library.dll");

    //Environment (2)
    System.load("C:\\cygwin64\\home\\mdorey\\tmp\\libc_jni_library.dll");
    }
    public native void fnDisplayData();
    public static void main(String[] args) {
        AvajJNI obj = new AvajJNI();
        obj.fnDisplayData();
    }
}

mdorey@VXD0141 ~/tmp
$ cat AvajJNI.c 
#include "stdio.h"

typedef unsigned long long __int64;
#include "avajjni_AvajJNI.h"

JNIEXPORT void JNICALL Java_avajjni_AvajJNI_fnDisplayData
  (JNIEnv *env, jobject obj){
    printf("JNI Method Called\n");
}

mdorey@VXD0141 ~/tmp
$ javac avajjni/AvajJNI.java

mdorey@VXD0141 ~/tmp
$ javah avajjni.AvajJNI

mdorey@VXD0141 ~/tmp
$ gcc -I /cygdrive/c/Program\ Files/Java/jdk1.8.0_72/include -I /cygdrive/c/Pro
gram\ Files/Java/jdk1.8.0_72/include/win32 -o libc_jni_library.dll -shared Avaj
JNI.c 

mdorey@VXD0141 ~/tmp
$ java avajjni/AvajJNI
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180185145, pid=8780, tid=7560
#
# JRE version: Java(TM) SE Runtime Environment (8.0_72-b15) (build 1.8.0_72-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.72-b15 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [cygwin1.dll+0x145145]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\cygwin64\home\mdorey\tmp\hs_err_pid8780.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

mdorey@VXD0141 ~/tmp
$ /cygdrive/c/Program\ Files/software.jessies.org/terminator/Resources/salma-ha
yek/.generated/amd64_Cygwin/bin/java-launcher.exe avajjni/AvajJNI
JNI Method Called

mdorey@VXD0141 ~/tmp
$ 

https://stackoverflow.com/a/13640189/18096