Java JNI EXCEPTION_ACCESS_VIOLATION

Java JNI EXCEPTION_ACCESS_VIOLATION

我是 JNI 的新手,正在尝试使用 Windows 7 和 cygwin 的 gcc 编译器来创建一个简单的 Hello World 程序。这是各种组件:

Hello.java:

public class Hello {
    public static native void sayHello();

    public static void main( String[] args ) {
        System.loadLibrary( "Hello" );
        sayHello();
    } // end main
} // end class Hello

然后我执行以下命令列表来构建 .h 文件:

javac Hello.java
javah Hello

这会自动构建 Hello.h 文件。为了完整起见,就在这里:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Hello */

#ifndef _Included_Hello
#define _Included_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Hello
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Hello_sayHello
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

然后我构建相应的Hello.c文件:

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

JNIEXPORT void JNICALL Java_Hello_sayHello( JNIEnv *env, jclass c ) {
    printf( "Hello World\n" );
}

然后,我 compile/link 使用以下命令的 c 代码:

gcc -I/cygdrive/c/Java/jdk1.8.0_45/include -I/cygdrive/c/Java/jdk1.8.0_45/include/win32 -Wl,--add-stdcall-alias -shared -o Hello.dll Hello.c

最后,我重新编译 java 文件。 (我真的需要这样做吗?):

javac Hello.java

我正在使用这个 Makefile(我对构建 Makefile 也比较陌生)通过一个 make 命令完成所有步骤:

CC=gcc
CFLAGS=
LDFLAGS=
JAVALIB="/cygdrive/C/Java/jdk1.8.0_45/include"
JAVALIBWIN32="$(JAVALIB)/win32"

all: Hello.dll
        javac Hello.java

Hello.dll: Hello.h
        $(CC) -I$(JAVALIB) -I$(JAVALIBWIN32) -Wl,--add-stdcall-alias -shared -o Hello.dll Hello.c


Hello.h: Hello.class
        rm -f Hello.h
        javah Hello

Hello.class: Hello.java
        javac Hello.java

clean:
        @rm -f *.dll *.class

一旦我执行程序(使用命令"java Hello"),我收到这个错误:

$ java Hello
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000001800f9309, pid=7292, tid=5616
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b14) (build 1.8.0_45-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [cygwin1.dll+0xb9309]
#
# 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:\dev\JNI\hs_err_pid7292.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.
#

并且,生成的错误日志如下所示:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000001800f9309, pid=7292, tid=5616
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b14) (build 1.8.0_45-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [cygwin1.dll+0xb9309]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# 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.
#

---------------  T H R E A D  ---------------

Current thread (0x000000000253e000):  JavaThread "main" [_thread_in_native, id=5616, stack(0x0000000002830000,0x0000000002930000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x0000000000000030

Registers:
RAX=0x00000000002c0000, RBX=0x0000000000000001, RCX=0x00000000002c0000, RDX=0x0000000000000000
RSP=0x000000000292f0e0, RBP=0x000000000292f1d0, RSI=0x000000000292f14c, RDI=0x000000000292f14c
R8 =0x000000000292f14c, R9 =0x8080808080808080, R10=0xfefefefefefefeff, R11=0x0000000551bc10ac
R12=0x0000000000000000, R13=0x000000000cf70258, R14=0x000000000292f258, R15=0x000000000253e000
RIP=0x00000001800f9309, EFLAGS=0x0000000000010206

Top of Stack: (sp=0x000000000292f0e0)
0x000000000292f0e0:   000000000cf70258 0000000000000000
0x000000000292f0f0:   000000000292f1b0 00000000701e8e37
0x000000000292f100:   000000000292f1b0 0000003200390032
0x000000000292f110:   000000000292d680 0000000180185145
0x000000000292f120:   000000000cf70258 000000000253e000
0x000000000292f130:   000000000253e000 0000000002577cd0
0x000000000292f140:   000000000cf70258 000000000292f258
0x000000000292f150:   000000000292f170 0000000000000002
0x000000000292f160:   000000000000000c 000000007013a904
0x000000000292f170:   0000000551bc3030 000000000000000b
0x000000000292f180:   00000001802653c0 0000000000000001
0x000000000292f190:   000000000cf70258 0000000040000020
0x000000000292f1a0:   000000000f017ced 000000018011ae5b
0x000000000292f1b0:   0000000040000000 000000000253e000
0x000000000292f1c0:   000000000cf70258 0000000040000020
0x000000000292f1d0:   000000000292f240 000000000f025e34 

Instructions: (pc=0x00000001800f9309)
0x00000001800f92e9:   1f 84 00 00 00 00 00 56 53 48 83 ec 28 89 cb 48
0x00000001800f92f9:   89 d6 e8 d0 4b 04 00 48 8b 10 49 89 f0 48 89 c1
0x00000001800f9309:   4c 8b 4a 30 89 da 48 83 c4 28 5b 5e 49 ff e1 0f
0x00000001800f9319:   1f 84 00 00 00 00 00 56 53 48 83 ec 28 89 cb 48 


Register to memory mapping:

RAX=0x00000000002c0000 is an unknown value
RBX=0x0000000000000001 is an unknown value
RCX=0x00000000002c0000 is an unknown value
RDX=0x0000000000000000 is an unknown value
RSP=0x000000000292f0e0 is pointing into the stack for thread: 0x000000000253e000
RBP=0x000000000292f1d0 is pointing into the stack for thread: 0x000000000253e000
RSI=0x000000000292f14c is pointing into the stack for thread: 0x000000000253e000
RDI=0x000000000292f14c is pointing into the stack for thread: 0x000000000253e000
R8 =0x000000000292f14c is pointing into the stack for thread: 0x000000000253e000
R9 =0x8080808080808080 is an unknown value
R10=0xfefefefefefefeff is an unknown value
R11=0x0000000551bc10ac is an unknown value
R12=0x0000000000000000 is an unknown value
R13={method} {0x000000000cf70260} 'sayHello' '()V' in 'Hello'
R14=0x000000000292f258 is pointing into the stack for thread: 0x000000000253e000
R15=0x000000000253e000 is a thread


Stack: [0x0000000002830000,0x0000000002930000],  sp=0x000000000292f0e0,  free space=1020k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [cygwin1.dll+0xb9309]
C  [cygwin1.dll+0x145145]
C  [cygwin1.dll+0xdae5a]
C  0x0000000040000000

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  Hello.sayHello()V+0
j  Hello.main([Ljava/lang/String;)V+5
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x000000001e093800 JavaThread "Service Thread" daemon [_thread_blocked, id=8452, stack(0x000000000ecc0000,0x000000000edc0000)]
  0x000000001e011000 JavaThread "C1 CompilerThread2" daemon [_thread_blocked, id=2924, stack(0x000000001e460000,0x000000001e560000)]
  0x000000000d56f000 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=7784, stack(0x000000000ee60000,0x000000000ef60000)]
  0x000000000d566800 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=6812, stack(0x000000000eba0000,0x000000000eca0000)]
  0x000000000d563000 JavaThread "Attach Listener" daemon [_thread_blocked, id=3936, stack(0x000000000e9c0000,0x000000000eac0000)]
  0x000000000d561800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=432, stack(0x000000000e590000,0x000000000e690000)]
  0x000000000d507000 JavaThread "Finalizer" daemon [_thread_blocked, id=7632, stack(0x000000000e8b0000,0x000000000e9b0000)]
  0x000000000d500000 JavaThread "Reference Handler" daemon [_thread_blocked, id=4284, stack(0x000000000e690000,0x000000000e790000)]
=>0x000000000253e000 JavaThread "main" [_thread_in_native, id=5616, stack(0x0000000002830000,0x0000000002930000)]

Other Threads:
  0x000000000d4fa800 VMThread [stack: 0x000000000e490000,0x000000000e590000] [id=8520]
  0x000000001e097800 WatcherThread [stack: 0x000000001e620000,0x000000001e720000] [id=3404]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap:
 PSYoungGen      total 76288K, used 2621K [0x000000076b200000, 0x0000000770700000, 0x00000007c0000000)
  eden space 65536K, 4% used [0x000000076b200000,0x000000076b48f680,0x000000076f200000)
  from space 10752K, 0% used [0x000000076fc80000,0x000000076fc80000,0x0000000770700000)
  to   space 10752K, 0% used [0x000000076f200000,0x000000076f200000,0x000000076fc80000)
 ParOldGen       total 175104K, used 0K [0x00000006c1600000, 0x00000006cc100000, 0x000000076b200000)
  object space 175104K, 0% used [0x00000006c1600000,0x00000006c1600000,0x00000006cc100000)
 Metaspace       used 2341K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 261K, capacity 386K, committed 512K, reserved 1048576K

Card table byte_map: [0x0000000002fc0000,0x00000000037c0000] byte_map_base: 0xffffffffff9b5000

Marking Bits: (ParMarkBitMap*) 0x0000000070814040
 Begin Bits: [0x0000000004420000, 0x00000000083c8000)
 End Bits:   [0x00000000083c8000, 0x000000000c370000)

Polling page: 0x0000000000250000

CodeCache: size=245760Kb used=1084Kb max_used=1084Kb free=244675Kb
 bounds [0x000000000f010000, 0x000000000f280000, 0x000000001e010000]
 total_blobs=247 nmethods=17 adapters=144
 compilation: enabled

Compilation events (10 events):
Event: 0.050 Thread 0x000000001e011000    6       3       java.lang.AbstractStringBuilder::ensureCapacityInternal (16 bytes)
Event: 0.050 Thread 0x000000001e011000 nmethod 6 0x000000000f11bad0 code [0x000000000f11bc40, 0x000000000f11be88]
Event: 0.050 Thread 0x000000001e011000   14       3       java.lang.AbstractStringBuilder::append (29 bytes)
Event: 0.051 Thread 0x000000001e011000 nmethod 14 0x000000000f11d250 code [0x000000000f11d3c0, 0x000000000f11d6c8]
Event: 0.051 Thread 0x000000001e011000   15  s    3       java.lang.StringBuffer::append (13 bytes)
Event: 0.051 Thread 0x000000001e011000 nmethod 15 0x000000000f11d850 code [0x000000000f11d9e0, 0x000000000f11dee8]
Event: 0.051 Thread 0x000000001e011000   16       3       java.io.WinNTFileSystem::isSlash (18 bytes)
Event: 0.051 Thread 0x000000001e011000 nmethod 16 0x000000000f11e110 code [0x000000000f11e260, 0x000000000f11e450]
Event: 0.055 Thread 0x000000001e011000   17       3       java.lang.String::equals (81 bytes)
Event: 0.055 Thread 0x000000001e011000 nmethod 17 0x000000000f11e8d0 code [0x000000000f11ea60, 0x000000000f11efb0]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (2 events):
Event: 0.026 Thread 0x000000000253e000 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000076b20cdf0) thrown at [C:\re\workspace-2-build-windows-amd64-cygwin\jdk8u4557\hotspot\srƒ­fÎÝ‘š?
Event: 0.026 Thread 0x000000000253e000 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000076b20d080) thrown at [C:\re\workspace-2-build-windows-amd64-cygwin\jdk8u4557\hotspot\src\share\vm\prims\jÐW

Events (10 events):
Event: 0.054 loading class java/security/BasicPermissionCollection
Event: 0.054 loading class java/security/BasicPermissionCollection done
Event: 0.054 loading class sun/launcher/LauncherHelper$FXHelper
Event: 0.054 loading class sun/launcher/LauncherHelper$FXHelper done
Event: 0.054 loading class java/lang/Class$MethodArray
Event: 0.054 loading class java/lang/Class$MethodArray done
Event: 0.054 loading class java/lang/Void
Event: 0.054 loading class java/lang/Void done
Event: 0.054 loading class java/lang/ClassLoaderHelper
Event: 0.054 loading class java/lang/ClassLoaderHelper done


Dynamic libraries:
0x000000013fbd0000 - 0x000000013fc07000     C:\Java\jdk1.8.0_45\bin\java.exe
0x0000000076df0000 - 0x0000000076f98000     C:\Windows\SYSTEM32\ntdll.dll
0x0000000076bd0000 - 0x0000000076cef000     C:\Windows\system32\kernel32.dll
0x000007fefcdd0000 - 0x000007fefce3c000     C:\Windows\system32\KERNELBASE.dll
0x00000000748b0000 - 0x0000000074945000     C:\Windows\System32\SYSFER.DLL
0x000007fefe7e0000 - 0x000007fefe8bb000     C:\Windows\system32\ADVAPI32.dll
0x000007fefdd00000 - 0x000007fefdd9f000     C:\Windows\system32\msvcrt.dll
0x000007fefeb10000 - 0x000007fefeb2f000     C:\Windows\SYSTEM32\sechost.dll
0x000007fefe6b0000 - 0x000007fefe7dd000     C:\Windows\system32\RPCRT4.dll
0x0000000076cf0000 - 0x0000000076dea000     C:\Windows\system32\USER32.dll
0x000007fefeaa0000 - 0x000007fefeb07000     C:\Windows\system32\GDI32.dll
0x000007fefe640000 - 0x000007fefe64e000     C:\Windows\system32\LPK.dll
0x000007fefef90000 - 0x000007feff059000     C:\Windows\system32\USP10.dll
0x000007fefb510000 - 0x000007fefb704000     C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_fa396087175ac9ac\COMCTL32.dll
0x000007fefe000000 - 0x000007fefe071000     C:\Windows\system32\SHLWAPI.dll
0x000007fefe320000 - 0x000007fefe34e000     C:\Windows\system32\IMM32.DLL
0x000007fefe080000 - 0x000007fefe189000     C:\Windows\system32\MSCTF.dll
0x000007fefcae0000 - 0x000007fefcb11000     C:\Windows\system32\nvinitx.dll
0x000007fefcad0000 - 0x000007fefcadc000     C:\Windows\system32\VERSION.dll
0x000000000f000000 - 0x000000000f006000     C:\Program Files\NVIDIA Corporation\CoProcManager\detoured.dll
0x000007fef9e90000 - 0x000007fef9ec1000     C:\Program Files\NVIDIA Corporation\CoProcManager\nvd3d9wrapx.dll
0x000007fefe8c0000 - 0x000007fefea97000     C:\Windows\system32\SETUPAPI.dll
0x000007fefcd90000 - 0x000007fefcdc6000     C:\Windows\system32\CFGMGR32.dll
0x000007fefe230000 - 0x000007fefe307000     C:\Windows\system32\OLEAUT32.dll
0x000007fefddf0000 - 0x000007fefdff3000     C:\Windows\system32\ole32.dll
0x000007fefce40000 - 0x000007fefce5a000     C:\Windows\system32\DEVOBJ.dll
0x000007fef9e60000 - 0x000007fef9e82000     C:\Program Files\NVIDIA Corporation\CoProcManager\nvdxgiwrapx.dll
0x000000005b3d0000 - 0x000000005b4a2000     C:\Java\jdk1.8.0_45\bin\msvcr100.dll
0x0000000070010000 - 0x0000000070893000     C:\Java\jdk1.8.0_45\bin\server\jvm.dll
0x000007fef6650000 - 0x000007fef6659000     C:\Windows\system32\WSOCK32.dll
0x000007fefdda0000 - 0x000007fefdded000     C:\Windows\system32\WS2_32.dll
0x000007fefe310000 - 0x000007fefe318000     C:\Windows\system32\NSI.dll
0x000007fefa7c0000 - 0x000007fefa7fb000     C:\Windows\system32\WINMM.dll
0x0000000076fc0000 - 0x0000000076fc7000     C:\Windows\system32\PSAPI.DLL
0x0000000071370000 - 0x000000007137f000     C:\Java\jdk1.8.0_45\bin\verify.dll
0x0000000071170000 - 0x0000000071199000     C:\Java\jdk1.8.0_45\bin\java.dll
0x0000000066750000 - 0x0000000066766000     C:\Java\jdk1.8.0_45\bin\zip.dll
0x000007fefcf70000 - 0x000007fefdcf9000     C:\Windows\system32\SHELL32.dll
0x000007fefcbb0000 - 0x000007fefcbbf000     C:\Windows\system32\profapi.dll
0x0000000551bc0000 - 0x0000000551bd8000     C:\dev\JNI\Hello.dll
0x0000000180040000 - 0x0000000180600000     C:\cygwin64\bin\cygwin1.dll
0x000007fef40d0000 - 0x000007fef41f5000     C:\Windows\system32\dbghelp.dll

VM Arguments:
java_command: Hello
java_class_path (initial): .
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Java\jdk1.8.0_45
PATH=C:\cygwin64\usr\local\bin;C:\cygwin64\bin;C:\ProgramData\Oracle\Java\javapath;C:\Java\jdk1.8.0_45\bin;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\cygwin64\bin;C:\bin;.;C:\cygwin64\lib\lapack
USERNAME=gary.valley
SHELL=/bin/bash
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 69 Stepping 1, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows 7 , 64 bit Build 7601 (6.1.7601.18409)

CPU:total 4 (2 cores per cpu, 2 threads per core) family 6 model 69 stepping 1, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, avx2, aes, clmul, erms, lzcnt, ht, tsc, tscinvbit, bmi1, bmi2

Memory: 4k page, physical 16680200k(12576164k free), swap 33358544k(28455468k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.45-b02) for windows-amd64 JRE (1.8.0_45-b14), built on Apr 10 2015 10:34:15 by "java_re" with MS VC++ 10.0 (VS2010)

time: Thu Apr 23 22:46:03 2015
elapsed time: 0 seconds (0d 0h 0m 0s)

我一直在逐行遵循此 site 中的指南。我唯一无法复制的是 -mno-cygwin 选项,因为我使用的是 gcc 版本 4.9.2 并且该选项无法识别。

谁能指出我正确的方向?非常感谢!

这肯定是Cygwin环境。我转移到 Ubuntu 发行版,链接的指南如广告所示工作。