Java – JNI with C keeps crashing cout, printf and method calls
I am writing my first JNI program in C and have developed test methods to ensure that everything is normal
The problem is: when I call a method test, it produces very volatile results Sometimes it runs forever, sometimes I get build errors, sometimes I get fatal errors and error messages This happens not only on printf and STD:: cout, but also on method calls (and then I always encounter fatal errors. I didn't include this example in the problem) I can't see the mistake I hope to get some help
I currently use G as the compiler and have other options: - D__ int64 = int64_ t.
Java classes:
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-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,build fails,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
Class 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
Errors during construction:
\nbproject\build-impl.xml:1051: The following error occurred while executing this line: \nbproject\build-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-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
00070
Registers:
RAX=0x0000000000000000,RBX=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-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
00070,RCX=0x0000000000000000,RDX=0x0000000000000000
RSP=0x000000000220f460,RBP=0x000000000220f730,RSI=0x00000003fefac4f0,RDI=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-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
000b0
R8 =0x0000000000000000,R9 =0x0000000000000000,R10=0x0000000000000000,R11=0x0000000000000000
R12=0x0000000000000000,R13=0x000000000000000a,R14=0x000000000220f670,R15=0x00000005696e3030
RIP=0x00007ffb434f1f28,EFLAGS=0x0000000000000206Top of Stack: (sp=0x000000000220f460)
0x000000000220f460: 00000000ffffffff 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
00070
0x000000000220f470: 00000003fefac4f0 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
000b0
0x000000000220f480: 0000000020474343 0000000000000000
0x000000000220f490: 00007ffb434f1f28 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
00001
0x000000000220f4a0: 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
00070 000000000000000a
0x000000000220f4b0: 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
00300 00000001800ce32c
0x000000000220f4c0: 00000001801523c1 000000000220f480
0x000000000220f4d0: 00000003fefbfec0 0000000180156860
0x000000000220f4e0: 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
001e0 0000000000000001
0x000000000220f4f0: 00000000000000b0 000000000220f730
0x000000000220f500: 0000000000000000 0000000000000000
0x000000000220f510: 0000000000000008 00000003ffa4d5ea
0x000000000220f520: 000077d3bf56b135 00000003fefbfec0
0x000000000220f530: 000000000220f4e0 00000003ffa4cc97
0x000000000220f540: 000000public class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
000b0 00000003fefac4f0
0x000000000220f550: 00000003fefcbcd0 000000000220f730Instructions: (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 ccRegister to memory mapping:
RAX=0x0000000000000000 is an unkNown value
RBX=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-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.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=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-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.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 valueStack: [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 0x0000000002375e34Java 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 1048576KCard 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: enabledCompilation 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.Sy@R_301_2354@::getSecurityManager (4 bytes)
Event: 0.082 Thread 0x0000000016e97000 nmethod 26 0x0000000002473950 code [0x0000000002473aa0,0x0000000002473bd0]GC Heap History (0 events):
No eventsDeoptimization events (0 events):
No eventsInternal 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:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u60\4407\hotspot\srÃö×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:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u60\4407\hotspot\src\share\vm\prims\jEvents (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 doneDynamic libraries:
0x00007ff69bpublic class JNI { public static native int test(); public static native int test1(int a); public static void main(String[] args) { String filename = "Path-to-lib\\libProjectJNI.dll"; Sy@R_301_2354@.load(filename); //Prints 10 - every time - as expected Sy@R_301_2354@.out.println(test1(5)); //Hangs,or Fatal error every time. Sy@R_301_2354@.out.println(test()); } }
0 - 0x00007ff69b097000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\java.exe
0x00007ffb465f0000 - 0x00007ffb467b1000 C:\WINDOWS\SY@R_301_2354@32\ntdll.dll
0x00007ffb45170000 - 0x00007ffb4521d000 C:\WINDOWS\sy@R_301_2354@32\KERNEL32.DLL
0x00007ffb43480000 - 0x00007ffb43668000 C:\WINDOWS\sy@R_301_2354@32\KERNELBASE.dll
0x00007ffb45850000 - 0x00007ffb458f7000 C:\WINDOWS\sy@R_301_2354@32\ADVAPI32.dll
0x00007ffb452f0000 - 0x00007ffb4538d000 C:\WINDOWS\sy@R_301_2354@32\msvcrt.dll
0x00007ffb45530000 - 0x00007ffb4558b000 C:\WINDOWS\sy@R_301_2354@32\sechost.dll
0x00007ffb46020000 - 0x00007ffb4613c000 C:\WINDOWS\sy@R_301_2354@32\RPCRT4.dll
0x00007ffb45e60000 - 0x00007ffb45fb6000 C:\WINDOWS\sy@R_301_2354@32\USER32.dll
0x00007ffb45390000 - 0x00007ffb45516000 C:\WINDOWS\sy@R_301_2354@32\GDI32.dll
0x00007ffb3e860000 - 0x00007ffb3ead4000 C:\WINDOWS\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.10586.0_none_8c15ae12515e1c22\COMCTL32.dll
0x00007ffb45a20000 - 0x00007ffb45c9d000 C:\WINDOWS\sy@R_301_2354@32\combase.dll
0x00007ffb43840000 - 0x00007ffb438aa000 C:\WINDOWS\sy@R_301_2354@32\bcryptPrimitives.dll
0x00007ffb45fe0000 - 0x00007ffb4601b000 C:\WINDOWS\sy@R_301_2354@32\IMM32.DLL
0x00000000726a0000 - 0x0000000072772000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\msvcr100.dll
0x00000000702c0000 - 0x0000000070b49000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\server\jvm.dll
0x00007ffb45520000 - 0x00007ffb45528000 C:\WINDOWS\sy@R_301_2354@32\PSAPI.DLL
0x00007ffb40550000 - 0x00007ffb40573000 C:\WINDOWS\SY@R_301_2354@32\WINMM.dll
0x00007ffb37a00000 - 0x00007ffb37a09000 C:\WINDOWS\SY@R_301_2354@32\WSOCK32.dll
0x00007ffb412a0000 - 0x00007ffb412aa000 C:\WINDOWS\SY@R_301_2354@32\VERSION.dll
0x00007ffb459b0000 - 0x00007ffb45a1b000 C:\WINDOWS\sy@R_301_2354@32\WS2_32.dll
0x00007ffb404f0000 - 0x00007ffb4051c000 C:\WINDOWS\SY@R_301_2354@32\WINMMBASE.dll
0x00007ffb42de0000 - 0x00007ffb42e23000 C:\WINDOWS\sy@R_301_2354@32\cfgmgr32.dll
0x0000000072650000 - 0x000000007265f000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\verify.dll
0x0000000072620000 - 0x0000000072649000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\java.dll
0x0000000072600000 - 0x0000000072616000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\zip.dll
0x00007ffb439f0000 - 0x00007ffb44f4c000 C:\WINDOWS\sy@R_301_2354@32\SHELL32.dll
0x00007ffb42e30000 - 0x00007ffb43475000 C:\WINDOWS\sy@R_301_2354@32\windows.storage.dll
0x00007ffb46140000 - 0x00007ffb46192000 C:\WINDOWS\sy@R_301_2354@32\shlwapi.dll
0x00007ffb42c60000 - 0x00007ffb42c6f000 C:\WINDOWS\sy@R_301_2354@32\kernel.appcore.dll
0x00007ffb42d20000 - 0x00007ffb42dd5000 C:\WINDOWS\sy@R_301_2354@32\shcore.dll
0x00007ffb42c10000 - 0x00007ffb42c5b000 C:\WINDOWS\sy@R_301_2354@32\powrprof.dll
0x00007ffb42c70000 - 0x00007ffb42c84000 C:\WINDOWS\sy@R_301_2354@32\profapi.dll
0x00000005696e0000 - 0x0000000569704000 C:\Users\active-folder-jni\dist\Debug\Cygwin-Windows\libProjectJNI.dll
0x0000000180040000 - 0x0000000180640000 C:\cygwin64\bin\cygwin1.dll
0x00000003fef00000 - 0x00000003ff055000 C:\cygwin64\bin\cygstdc++-6.dll
0x00000003ffa40000 - 0x00000003ffa5a000 C:\cygwin64\bin\cyggcc_s-seh-1.dll
0x00007ffb2c030000 - 0x00007ffb2c1bc000 C:\WINDOWS\SY@R_301_2354@32\dbghelp.dllVM Arguments:
jvm_args: -Dfile.encoding=UTF-8
java_command: gol.s305089.JNI
java_class_path (initial): C:\Users\active-folder-java-src\dist\Project.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\plugin.jar;C:\Users\active-folder-java-src\build\classes
Launcher Type: SUN_STANDARDEnvironment Variables:
PATH=C:\Program Files (x86)\iis express\PHP\v5.3;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\sy@R_301_2354@32;C:\WINDOWS;C:\WINDOWS\Sy@R_301_2354@32\Wbem;C:\WINDOWS\Sy@R_301_2354@32\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\Microsoft\Web Platform Installer\;C:\Program Files\MATLAB\R2015b\runtime\win64;C:\Program Files\MATLAB\R2015b\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\cygwin64\bin;C:\Program Files\Java\jdk1.8.0_60\bin
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-lib\\libProjectJNI.dll";
Sy@R_301_2354@.load(filename);//Prints 10 - every time - as expected
Sy@R_301_2354@.out.println(test1(5));//Hangs,or Fatal error every time.
Sy@R_301_2354@.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-lib\\libProjectJNI.dll";
Sy@R_301_2354@.load(filename);//Prints 10 - every time - as expected
Sy@R_301_2354@.out.println(test1(5));//Hangs,or Fatal error every time.
Sy@R_301_2354@.out.println(test());
}
}time: Sat Apr 30 21:52:33 2016
elapsed time: 0 seconds (0d 0h 0m 0s)Solution
It is so simple because the C side gives an 8-byte integer address, but Java thinks the int type you receive is a 4-byte integer?
The Java 8 SDK states that int is 32 bits https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
Because you return a (possibly 64 - bit) integer from C, Java "converts" it to a 32 - bit type Try changing the return type of the Java side to long to see if it is valid
public static native long test1(int a);Also, why not try the following to see if you can tell where the crash occurred?
long n = test1(5); // get the value into a 64 bit int Sy@R_301_2354@.out.format("%d%n",n); // print it separately