我正在用c编写我的第一个JNI程序,并制定了测试方法以确保一切正常.
问题是:当我调用方法测试时,它会产生非常波动的结果.
有时它会永远运行,有时我会收到构建错误,有时我会收到致命错误和错误消息.这不仅发生在printf和std :: cout上,而且还发生在方法调用上(然后我总是遇到致命的错误.我没有在问题中包含这个例子).
我看不出错误.希望得到一些帮助.
我目前使用g作为编译器,还有其他选项:-D__int64 = int64_t.
Java类:
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,build fails,or fatal error every time.
System.out.println(test());
}
}
C类:
#include "JNI.h"
#include <iostream>
#include <string>
JNIEXPORT jint JNICALL Java_project_JNI_test
(JNIEnv * env,jclass obj) {
std::cout << "PRINT THIS";
return 10;
}
JNIEXPORT jint JNICALL Java_project_JNI_test1
(JNIEnv *env,jclass obj,jint aint) {
return aint * 2;
}
C h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class project_JNI */
#ifndef _Included_project_JNI
#define _Included_project_JNI
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_project_JNI_test
(JNIEnv *,jclass);
JNIEXPORT jint JNICALL Java_project_JNI_test1
(JNIEnv *,jclass,jint);
#ifdef __cplusplus
}
#endif
#endif
构建一段时间发生的错误:
nbprojectbuild-impl.xml:1051: The following error occurred while executing this line:
nbprojectbuild-impl.xml:805: Java returned: -1073741819
BUILD FAILED (total time: 0 seconds)
hs_err_pid ### log
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (0x20474343),pid=33944,tid=3260
#
# JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [KERNELBASE.dll+0x71f28]
#
# 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 (0x0000000002260800): JavaThread "main" [_thread_in_native,id=3260,stack(0x0000000002110000,0x0000000002210000)]
siginfo: ExceptionCode=0x20474343,ExceptionInformation=0x000000public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00070
Registers:
RAX=0x0000000000000000,RBX=0x000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00070,RCX=0x0000000000000000,RDX=0x0000000000000000
RSP=0x000000000220f460,RBP=0x000000000220f730,RSI=0x00000003fefac4f0,RDI=0x000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
000b0
R8 =0x0000000000000000,R9 =0x0000000000000000,R10=0x0000000000000000,R11=0x0000000000000000
R12=0x0000000000000000,R13=0x000000000000000a,R14=0x000000000220f670,R15=0x00000005696e3030
RIP=0x00007ffb434f1f28,EFLAGS=0x0000000000000206
Top of Stack: (sp=0x000000000220f460)
0x000000000220f460: 00000000ffffffff 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00070
0x000000000220f470: 00000003fefac4f0 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
000b0
0x000000000220f480: 0000000020474343 0000000000000000
0x000000000220f490: 00007ffb434f1f28 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00001
0x000000000220f4a0: 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00070 000000000000000a
0x000000000220f4b0: 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00300 00000001800ce32c
0x000000000220f4c0: 00000001801523c1 000000000220f480
0x000000000220f4d0: 00000003fefbfec0 0000000180156860
0x000000000220f4e0: 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
001e0 0000000000000001
0x000000000220f4f0: 00000000000000b0 000000000220f730
0x000000000220f500: 0000000000000000 0000000000000000
0x000000000220f510: 0000000000000008 00000003ffa4d5ea
0x000000000220f520: 000077d3bf56b135 00000003fefbfec0
0x000000000220f530: 000000000220f4e0 00000003ffa4cc97
0x000000000220f540: 000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
000b0 00000003fefac4f0
0x000000000220f550: 00000003fefcbcd0 000000000220f730
Instructions: (pc=0x00007ffb434f1f28)
0x00007ffb434f1f08: 49 8b d1 44 0f 47 c0 44 89 44 24 38 49 c1 e0 03
0x00007ffb434f1f18: e8 27 b2 00 00 48 8d 4c 24 20 ff 15 c8 a7 05 00
0x00007ffb434f1f28: 48 8b 8c 24 c0 00 00 00 48 33 cc e8 98 7a 00 00
0x00007ffb434f1f38: 48 81 c4 d8 00 00 00 c3 83 64 24 38 00 eb d6 cc
Register to memory mapping:
RAX=0x0000000000000000 is an unknown value
RBX=0x000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
00070 is an unknown value
RCX=0x0000000000000000 is an unknown value
RDX=0x0000000000000000 is an unknown value
RSP=0x000000000220f460 is pointing into the stack for thread: 0x0000000002260800
RBP=0x000000000220f730 is pointing into the stack for thread: 0x0000000002260800
RSI=0x00000003fefac4f0 is an unknown value
RDI=0x000000
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
000b0 is an unknown value
R8 =0x0000000000000000 is an unknown value
R9 =0x0000000000000000 is an unknown value
R10=0x0000000000000000 is an unknown value
R11=0x0000000000000000 is an unknown value
R12=0x0000000000000000 is an unknown value
R13=0x000000000000000a is an unknown value
R14=0x000000000220f670 is pointing into the stack for thread: 0x0000000002260800
R15=0x00000005696e3030 is an unknown value
Stack: [0x0000000002110000,0x0000000002210000],sp=0x000000000220f460,free space=1021k
Native frames: (J=compiled Java code,j=interpreted,Vv=VM code,C=native code)
C [KERNELBASE.dll+0x71f28]
C [cyggcc_s-seh-1.dll+0xcc97]
C [cygstdc++-6.dll+0xba6d1]
C [cygstdc++-6.dll+0xb0a7f]
C [cygstdc++-6.dll+0xada61]
C [cygstdc++-6.dll+0xaff59]
C [cygstdc++-6.dll+0xb4ee7]
C [libProjectJNI.dll+0x10b3]
C 0x0000000002375e34
Java frames: (J=compiled Java code,Vv=VM code)
j gol.s305089.JNI.test()I+0
j gol.s305089.JNI.main([Ljava/lang/String;)V+20
v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
0x00000000187fc800 JavaThread "Service Thread" daemon [_thread_blocked,id=2792,stack(0x0000000018bd0000,0x0000000018cd0000)]
0x0000000016e97000 JavaThread "C1 CompilerThread2" daemon [_thread_blocked,id=35452,stack(0x00000000186d0000,0x00000000187d0000)]
0x0000000016e8f800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked,id=15496,stack(0x00000000185d0000,0x00000000186d0000)]
0x0000000016e8d800 JavaThread "C2 CompilerThread0" daemon [_thread_blocked,id=17716,stack(0x00000000184d0000,0x00000000185d0000)]
0x0000000016e8b000 JavaThread "Attach Listener" daemon [_thread_blocked,id=26948,stack(0x00000000183d0000,0x00000000184d0000)]
0x0000000016e89800 JavaThread "Signal Dispatcher" daemon [_thread_blocked,id=9012,stack(0x00000000182d0000,0x00000000183d0000)]
0x0000000002356800 JavaThread "Finalizer" daemon [_thread_blocked,id=24532,stack(0x00000000180e0000,0x00000000181e0000)]
0x000000000234d800 JavaThread "Reference Handler" daemon [_thread_blocked,id=35052,stack(0x0000000017fe0000,0x00000000180e0000)]
=>0x0000000002260800 JavaThread "main" [_thread_in_native,0x0000000002210000)]
Other Threads:
0x0000000016e58000 VMThread [stack: 0x0000000017ee0000,0x0000000017fe0000] [id=4028]
0x0000000018803000 WatcherThread [stack: 0x0000000018cd0000,0x0000000018dd0000] [id=7356]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap:
PSYoungGen total 38400K,used 1996K [0x00000000d5f00000,0x00000000d8980000,0x0000000100000000)
eden space 33280K,6% used [0x00000000d5f00000,0x00000000d60f33f8,0x00000000d7f80000)
from space 5120K,0% used [0x00000000d8480000,0x00000000d8480000,0x00000000d8980000)
to space 5120K,0% used [0x00000000d7f80000,0x00000000d7f80000,0x00000000d8480000)
ParOldGen total 87552K,used 0K [0x0000000081c00000,0x0000000087180000,0x00000000d5f00000)
object space 87552K,0% used [0x0000000081c00000,0x0000000081c00000,0x0000000087180000)
Metaspace used 2783K,capacity 4486K,committed 4864K,reserved 1056768K
class space used 302K,capacity 386K,committed 512K,reserved 1048576K
Card table byte_map: [0x0000000011720000,0x0000000011b20000] byte_map_base: 0x0000000011312000
Marking Bits: (ParMarkBitMap*) 0x0000000070aca5b0
Begin Bits: [0x00000000121d0000,0x0000000014160000)
End Bits: [0x0000000014160000,0x00000000160f0000)
Polling page: 0x0000000000910000
CodeCache: size=245760Kb used=1102Kb max_used=1102Kb free=244657Kb
bounds [0x0000000002360000,0x00000000025d0000,0x0000000011360000]
total_blobs=260 nmethods=26 adapters=148
compilation: enabled
Compilation events (10 events):
Event: 0.079 Thread 0x0000000016e97000 22 3 java.util.jar.Attributes$Name::isAlpha (30 bytes)
Event: 0.079 Thread 0x0000000016e97000 nmethod 22 0x000000000246fb50 code [0x000000000246fcc0,0x000000000246fef0]
Event: 0.080 Thread 0x0000000016e97000 23 3 sun.misc.ASCIICaseInsensitiveComparator::toLower (16 bytes)
Event: 0.080 Thread 0x0000000016e97000 nmethod 23 0x0000000002472950 code [0x0000000002472ac0,0x0000000002472d10]
Event: 0.080 Thread 0x0000000016e97000 24 3 sun.misc.ASCIICaseInsensitiveComparator::isUpper (18 bytes)
Event: 0.080 Thread 0x0000000016e97000 nmethod 24 0x0000000002472dd0 code [0x0000000002472f20,0x00000000024730d0]
Event: 0.080 Thread 0x0000000016e97000 25 3 java.lang.String::getChars (62 bytes)
Event: 0.081 Thread 0x0000000016e97000 nmethod 25 0x0000000002473150 code [0x0000000002473300,0x0000000002473738]
Event: 0.082 Thread 0x0000000016e97000 26 3 java.lang.System::getSecurityManager (4 bytes)
Event: 0.082 Thread 0x0000000016e97000 nmethod 26 0x0000000002473950 code [0x0000000002473aa0,0x0000000002473bd0]
GC Heap History (0 events):
No events
Deoptimization events (0 events):
No events
Internal exceptions (2 events):
Event: 0.028 Thread 0x0000000002260800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x00000000d5f0da00) thrown at [C:reworkspace8-2-build-windows-amd64-cygwinjdk8u604407hotspotsr??×O ?
Event: 0.028 Thread 0x0000000002260800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x00000000d5f0dce8) thrown at [C:reworkspace8-2-build-windows-amd64-cygwinjdk8u604407hotspotsrcsharevmprimsj
Events (10 events):
Event: 0.081 loading class java/security/UnresolvedPermission
Event: 0.082 loading class java/security/UnresolvedPermission done
Event: 0.082 loading class java/security/BasicPermissionCollection
Event: 0.082 loading class java/security/BasicPermissionCollection done
Event: 0.082 loading class sun/launcher/LauncherHelper$FXHelper
Event: 0.082 loading class sun/launcher/LauncherHelper$FXHelper done
Event: 0.082 loading class java/lang/Class$MethodArray
Event: 0.082 loading class java/lang/Class$MethodArray done
Event: 0.082 loading class java/lang/Void
Event: 0.082 loading class java/lang/Void done
Dynamic libraries:
0x00007ff69b
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}
0 - 0x00007ff69b097000 C:Program FilesJavajdk1.8.0_60jrebinjava.exe
0x00007ffb465f0000 - 0x00007ffb467b1000 C:WINDOWSSYSTEM32ntdll.dll
0x00007ffb45170000 - 0x00007ffb4521d000 C:WINDOWSsystem32KERNEL32.DLL
0x00007ffb43480000 - 0x00007ffb43668000 C:WINDOWSsystem32KERNELBASE.dll
0x00007ffb45850000 - 0x00007ffb458f7000 C:WINDOWSsystem32ADVAPI32.dll
0x00007ffb452f0000 - 0x00007ffb4538d000 C:WINDOWSsystem32msvcrt.dll
0x00007ffb45530000 - 0x00007ffb4558b000 C:WINDOWSsystem32sechost.dll
0x00007ffb46020000 - 0x00007ffb4613c000 C:WINDOWSsystem32RPCRT4.dll
0x00007ffb45e60000 - 0x00007ffb45fb6000 C:WINDOWSsystem32USER32.dll
0x00007ffb45390000 - 0x00007ffb45516000 C:WINDOWSsystem32GDI32.dll
0x00007ffb3e860000 - 0x00007ffb3ead4000 C:WINDOWSWinSxSamd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.10586.0_none_8c15ae12515e1c22COMCTL32.dll
0x00007ffb45a20000 - 0x00007ffb45c9d000 C:WINDOWSsystem32combase.dll
0x00007ffb43840000 - 0x00007ffb438aa000 C:WINDOWSsystem32bcryptPrimitives.dll
0x00007ffb45fe0000 - 0x00007ffb4601b000 C:WINDOWSsystem32IMM32.DLL
0x00000000726a0000 - 0x0000000072772000 C:Program FilesJavajdk1.8.0_60jrebinmsvcr100.dll
0x00000000702c0000 - 0x0000000070b49000 C:Program FilesJavajdk1.8.0_60jrebinserverjvm.dll
0x00007ffb45520000 - 0x00007ffb45528000 C:WINDOWSsystem32PSAPI.DLL
0x00007ffb40550000 - 0x00007ffb40573000 C:WINDOWSSYSTEM32WINMM.dll
0x00007ffb37a00000 - 0x00007ffb37a09000 C:WINDOWSSYSTEM32WSOCK32.dll
0x00007ffb412a0000 - 0x00007ffb412aa000 C:WINDOWSSYSTEM32VERSION.dll
0x00007ffb459b0000 - 0x00007ffb45a1b000 C:WINDOWSsystem32WS2_32.dll
0x00007ffb404f0000 - 0x00007ffb4051c000 C:WINDOWSSYSTEM32WINMMBASE.dll
0x00007ffb42de0000 - 0x00007ffb42e23000 C:WINDOWSsystem32cfgmgr32.dll
0x0000000072650000 - 0x000000007265f000 C:Program FilesJavajdk1.8.0_60jrebinverify.dll
0x0000000072620000 - 0x0000000072649000 C:Program FilesJavajdk1.8.0_60jrebinjava.dll
0x0000000072600000 - 0x0000000072616000 C:Program FilesJavajdk1.8.0_60jrebinzip.dll
0x00007ffb439f0000 - 0x00007ffb44f4c000 C:WINDOWSsystem32SHELL32.dll
0x00007ffb42e30000 - 0x00007ffb43475000 C:WINDOWSsystem32windows.storage.dll
0x00007ffb46140000 - 0x00007ffb46192000 C:WINDOWSsystem32shlwapi.dll
0x00007ffb42c60000 - 0x00007ffb42c6f000 C:WINDOWSsystem32kernel.appcore.dll
0x00007ffb42d20000 - 0x00007ffb42dd5000 C:WINDOWSsystem32shcore.dll
0x00007ffb42c10000 - 0x00007ffb42c5b000 C:WINDOWSsystem32powrprof.dll
0x00007ffb42c70000 - 0x00007ffb42c84000 C:WINDOWSsystem32profapi.dll
0x00000005696e0000 - 0x0000000569704000 C:Usersactive-folder-jnidistDebugCygwin-WindowslibProjectJNI.dll
0x0000000180040000 - 0x0000000180640000 C:cygwin64bincygwin1.dll
0x00000003fef00000 - 0x00000003ff055000 C:cygwin64bincygstdc++-6.dll
0x00000003ffa40000 - 0x00000003ffa5a000 C:cygwin64bincyggcc_s-seh-1.dll
0x00007ffb2c030000 - 0x00007ffb2c1bc000 C:WINDOWSSYSTEM32dbghelp.dll
VM Arguments:
jvm_args: -Dfile.encoding=UTF-8
java_command: gol.s305089.JNI
java_class_path (initial): C:Usersactive-folder-java-srcdistProject.jar;C:Program FilesJavajdk1.8.0_60jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_60jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_60jrelibplugin.jar;C:Usersactive-folder-java-srcbuildclasses
Launcher Type: SUN_STANDARD
Environment Variables:
PATH=C:Program Files (x86)iis expressPHPv5.3;C:ProgramDataOracleJavajavapath;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program FilesMicrosoftWeb Platform Installer;C:Program FilesMATLABR2015bruntimewin64;C:Program FilesMATLABR2015bbin;C:Program FilesGitcmd;C:Program Files (x86)MiKTeX 2.9miktexbin;C:cygwin64bin;C:Program FilesJavajdk1.8.0_60bin
USERNAME=myUsername
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 60 Stepping 3,GenuineIntel
--------------- S Y S T E M ---------------
OS: Windows 10.0,64 bit Build 10586 (10.0.10586.0)
CPU:total 4 (2 cores per cpu,2 threads per core) family 6 model 60 stepping 3,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 8268824k(3297764k free),swap 9579544k(2510856k free)
vm_info: Java HotSpot(TM) 64-Bit Server VM (25.60-b23) for windows-amd64 JRE (1.8.0_60-b27),built on Aug 4 2015 11:06:27 by "java_re" with MS VC++ 10.0 (VS2010)
public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}public class JNI {
public static native int test();
public static native int test1(int a);
public static void main(String[] args) {
String filename = "Path-to-liblibProjectJNI.dll";
System.load(filename);
//Prints 10 - every time - as expected
System.out.println(test1(5));
//Hangs,or fatal error every time.
System.out.println(test());
}
}time: Sat Apr 30 21:52:33 2016
elapsed time: 0 seconds (0d 0h 0m 0s)