1

I am trying to get the most basic jni sample working.

I am following this tutorial, a little old, but the best I could find

Steps:

  • create the HelloJNI.java

    public class HelloJNI {
        static {
            System.loadLibrary("hello");
        }
        private native void sayHello();
    
        public static void main(String[] args) {
            new HelloJNI().sayHello();  // invoke the native method
        }
    }
    
  • Compile the HelloJNI.java into HelloJNI.class:

    javac HelloJNI.java

  • Generate the header by running:

    javah HelloJNI

    HelloJNI.h:

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class HelloJNI */
    
    #ifndef _Included_HelloJNI
    #define _Included_HelloJNI
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     HelloJNI
     * Method:    sayHello
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_HelloJNI_sayHello
      (JNIEnv *, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    
  • Write the HelloJNI.c file:

    #include <jni.h>
    #include <stdio.h>
    #include "HelloJNI.h"
    
    // Implementation of native method sayHello() of HelloJNI class
    JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
       printf("Hello World!\n");
       return;
    }
    

And now the fun starts...

First of all, %JAVA_HOME% does not work, you have to use $JAVA_HOME

  • set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_102
  • echo $JAVA_HOME
  • gcc -Wl,--add-stdcall-alias -I"$JAVA_HOME\include" -I"$JAVA_HOME\include\win32" -shared -o hello.dll HelloJNI.c

It looks like I made it... ehm no:

enter image description here

Then I gave a try with Cygwin:

gcc-3 -D __int64="long long" -mno-cygwin -Wl,--add-stdcall-alias -I"<JAVA_HOME>\include" -I"<JAVA_HOME>\include\win32" -shared -o hello.dll HelloJNI.c

-bash: gcc-3: command not found

Let me change gcc-3 to gcc:

gcc -D __int64="long long" -mno-cygwin -Wl,--add-stdcall-alias -I"<JAVA_HOME>\include" -I"<JAVA_HOME>\include\win32" -shared -o hello.dll HelloJNI.c

gcc: error: option -mno-cygwin unknown

Let's try to remove that

gcc -D __int64="long long" -Wl,--add-stdcall-alias -I"<JAVA_HOME>\include" -I"<JAVA_HOME>\include\win32" -shared -o hello.dll HelloJNI.c

no such file "jni.h"

let me change to $JAVA_HOME

gcc -D __int64="long long" -Wl,--add-stdcall-alias -I"$JAVA_HOME\include" -I"$JAVA_HOME\include\win32" -shared -o hello.dll HelloJNI.c

maybe this time it worked..

java HelloJNI hangs out

java -Djava.library.path=. HelloJNI segmentation fault

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180193180, pid=500, tid=0x0000000000000f10
#
# JRE version: Java(TM) SE Runtime Environment (8.0_102-b14) (build 1.8.0_102-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.102-b14 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [cygwin1.dll+0x153180]
#
# 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 (0x0000000001fee800):  JavaThread "main" [_thread_in_native, id=3856, stack(0x00000000022b0000,0x00000000023b0000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x0000000000000096

Registers:
RAX=0x0000000000000016, RBX=0x0000000000000002, RCX=0x0000000180300040, RDX=0x0000000000000001
RSP=0x00000000023af6f0, RBP=0x00000000023af7a0, RSI=0x00000000023ad680, RDI=0x00000000023af71c
R8 =0x00000000023af71c, R9 =0x000000018014ac50, R10=0xfefefefefefefeff, R11=0x00000004911210ac
R12=0x0000000000000000, R13=0x000000001c290280, R14=0x00000000023af838, R15=0x0000000001fee800
RIP=0x0000000180193180, EFLAGS=0x0000000000010206

Top of Stack: (sp=0x00000000023af6f0)
0x00000000023af6f0:   000000001c290280 0000000001fee800
0x00000000023af700:   0000000001fee800 0000000001fe2730
0x00000000023af710:   000000001c290280 00000000023af838
0x00000000023af720:   00000000023af740 0000000000000002
0x00000000023af730:   000000000000000d 0000000001fee800
0x00000000023af740:   0000000491123030 000000000000000c
0x00000000023af750:   000000018027fda0 0000000000000001
0x00000000023af760:   000000001c290280 0000000020000021
0x00000000023af770:   00000000ffffffff 000000018012700b
0x00000000023af780:   00000000023afa00 0000000001fee800
0x00000000023af790:   000000001c290280 0000000020000021
0x00000000023af7a0:   00000000023af818 0000000002a07f54
0x00000000023af7b0:   0000000001fee9f8 00000000023af838
0x00000000023af7c0:   0000000020000021 0000000001fee800
0x00000000023af7d0:   0000000002a07c82 00000000023af7d8
0x00000000023af7e0:   000000001c290280 00000000023af838 

Instructions: (pc=0x0000000180193180)
0x0000000180193160:   46 50 48 8b 5e 10 85 c0 0f 84 92 00 00 00 48 8d
0x0000000180193170:   7c 24 2c b9 01 00 00 00 48 89 fa e8 c0 0f f7 ff
0x0000000180193180:   f6 83 94 00 00 00 01 0f b7 43 10 75 05 f6 c4 02
0x0000000180193190:   74 7e f6 c4 20 75 11 81 a3 94 00 00 00 ff df ff 


Register to memory mapping:

RAX=0x0000000000000016 is an unknown value
RBX=0x0000000000000002 is an unknown value
RCX=0x0000000180300040 is an unknown value
RDX=0x0000000000000001 is an unknown value
RSP=0x00000000023af6f0 is pointing into the stack for thread: 0x0000000001fee800
RBP=0x00000000023af7a0 is pointing into the stack for thread: 0x0000000001fee800
RSI=0x00000000023ad680 is pointing into the stack for thread: 0x0000000001fee800
RDI=0x00000000023af71c is pointing into the stack for thread: 0x0000000001fee800
R8 =0x00000000023af71c is pointing into the stack for thread: 0x0000000001fee800
R9 =0x000000018014ac50 is an unknown value
R10=0xfefefefefefefeff is an unknown value
R11=0x00000004911210ac is an unknown value
R12=0x0000000000000000 is an unknown value
R13={method} {0x000000001c290288} 'sayHello' '()V' in 'HelloJNI'
R14=0x00000000023af838 is pointing into the stack for thread: 0x0000000001fee800
R15=0x0000000001fee800 is a thread


Stack: [0x00000000022b0000,0x00000000023b0000],  sp=0x00000000023af6f0,  free space=1021k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [cygwin1.dll+0x153180]
C  [cygwin1.dll+0xe700a]
C  0x00000000023afa00

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

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

Java Threads: ( => current thread )
  0x000000001d8b7800 JavaThread "Service Thread" daemon [_thread_blocked, id=5544, stack(0x000000001f6d0000,0x000000001f7d0000)]
  0x000000001d853800 JavaThread "C1 CompilerThread3" daemon [_thread_blocked, id=4884, stack(0x000000001f5d0000,0x000000001f6d0000)]
  0x000000001d833000 JavaThread "C2 CompilerThread2" daemon [_thread_blocked, id=3560, stack(0x000000001f310000,0x000000001f410000)]
  0x000000001d82a800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=5756, stack(0x000000001f060000,0x000000001f160000)]
  0x000000001d823800 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=3092, stack(0x000000001f1c0000,0x000000001f2c0000)]
  0x000000001d820000 JavaThread "Attach Listener" daemon [_thread_blocked, id=1712, stack(0x000000001ef20000,0x000000001f020000)]
  0x000000001c88f000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=2452, stack(0x000000001ed10000,0x000000001ee10000)]
  0x000000001c87f800 JavaThread "Finalizer" daemon [_thread_blocked, id=5552, stack(0x000000001e760000,0x000000001e860000)]
  0x000000001c878800 JavaThread "Reference Handler" daemon [_thread_blocked, id=4664, stack(0x000000001ea50000,0x000000001eb50000)]
=>0x0000000001fee800 JavaThread "main" [_thread_in_native, id=3856, stack(0x00000000022b0000,0x00000000023b0000)]

Other Threads:
  0x000000001c872000 VMThread [stack: 0x000000001e890000,0x000000001e990000] [id=5488]
  0x000000001d8e6800 WatcherThread [stack: 0x000000001f880000,0x000000001f980000] [id=5872]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap:
 PSYoungGen      total 76288K, used 2621K [0x000000076b180000, 0x0000000770680000, 0x00000007c0000000)
  eden space 65536K, 4% used [0x000000076b180000,0x000000076b40f5e8,0x000000076f180000)
  from space 10752K, 0% used [0x000000076fc00000,0x000000076fc00000,0x0000000770680000)
  to   space 10752K, 0% used [0x000000076f180000,0x000000076f180000,0x000000076fc00000)
 ParOldGen       total 175104K, used 0K [0x00000006c1400000, 0x00000006cbf00000, 0x000000076b180000)
  object space 175104K, 0% used [0x00000006c1400000,0x00000006c1400000,0x00000006cbf00000)
 Metaspace       used 2512K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 282K, capacity 386K, committed 512K, reserved 1048576K

Card table byte_map: [0x0000000011db0000,0x00000000125b0000] byte_map_base: 0x000000000e7a6000

Marking Bits: (ParMarkBitMap*) 0x000000005556a6c0
 Begin Bits: [0x0000000013730000, 0x00000000176e0000)
 End Bits:   [0x00000000176e0000, 0x000000001b690000)

Polling page: 0x0000000000110000

CodeCache: size=245760Kb used=1109Kb max_used=1113Kb free=244650Kb
 bounds [0x00000000029f0000, 0x0000000002c60000, 0x00000000119f0000]
 total_blobs=258 nmethods=27 adapters=145
 compilation: enabled

Compilation events (10 events):
Event: 0.044 Thread 0x000000001d853800 nmethod 22 0x0000000002b02210 code [0x0000000002b023c0, 0x0000000002b02af8]
Event: 0.044 Thread 0x000000001d853800   23       3       java.lang.AbstractStringBuilder::append (50 bytes)
Event: 0.044 Thread 0x000000001d853800 nmethod 23 0x0000000002b05b90 code [0x0000000002b05d60, 0x0000000002b064b8]
Event: 0.044 Thread 0x000000001d823800 nmethod 24 0x0000000002b067d0 code [0x0000000002b06920, 0x0000000002b069f8]
Event: 0.046 Thread 0x000000001d853800   25       3       java.lang.StringBuilder::append (8 bytes)
Event: 0.046 Thread 0x000000001d853800 nmethod 25 0x0000000002b05010 code [0x0000000002b05180, 0x0000000002b05308]
Event: 0.047 Thread 0x000000001d853800   26       3       java.lang.System::getSecurityManager (4 bytes)
Event: 0.047 Thread 0x000000001d853800 nmethod 26 0x0000000002b04d10 code [0x0000000002b04e60, 0x0000000002b04fb0]
Event: 0.047 Thread 0x000000001d853800   27       3       java.io.WinNTFileSystem::normalize (143 bytes)
Event: 0.048 Thread 0x000000001d853800 nmethod 27 0x0000000002b03f50 code [0x0000000002b04140, 0x0000000002b04968]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (3 events):
Event: 0.020 Thread 0x0000000001fee800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000076b187c78) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\sU-¡r#.”?
Event: 0.020 Thread 0x0000000001fee800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000076b187f60) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\src\share\vm\prims\3ßçÄ…*¥?
Event: 0.041 Thread 0x0000000001fee800 Exception <a 'java/io/FileNotFoundException'> (0x000000076b1dc428) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\src\share\vm\prims\jni.cpp, line 709]

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


Dynamic libraries:
0x000000013f9c0000 - 0x000000013f9f7000     C:\Program Files\Java\jre1.8.0_102\bin\java.exe
0x0000000077900000 - 0x0000000077aaa000     C:\Windows\SYSTEM32\ntdll.dll
0x00000000776e0000 - 0x00000000777ff000     C:\Windows\system32\kernel32.dll
0x000007fefd8e0000 - 0x000007fefd94a000     C:\Windows\system32\KERNELBASE.dll
0x000007fefda70000 - 0x000007fefdb4b000     C:\Windows\system32\ADVAPI32.dll
0x000007fefdfe0000 - 0x000007fefe07f000     C:\Windows\system32\msvcrt.dll
0x000007feff860000 - 0x000007feff87f000     C:\Windows\SYSTEM32\sechost.dll
0x000007fefdd90000 - 0x000007fefdebd000     C:\Windows\system32\RPCRT4.dll
0x0000000077800000 - 0x00000000778fa000     C:\Windows\system32\USER32.dll
0x000007feff8e0000 - 0x000007feff947000     C:\Windows\system32\GDI32.dll
0x000007feff950000 - 0x000007feff95e000     C:\Windows\system32\LPK.dll
0x000007fefdec0000 - 0x000007fefdf8a000     C:\Windows\system32\USP10.dll
0x000007fefbf90000 - 0x000007fefc184000     C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.18837_none_fa3b1e3d17594757\COMCTL32.dll
0x000007fefe0a0000 - 0x000007fefe111000     C:\Windows\system32\SHLWAPI.dll
0x000007fefe120000 - 0x000007fefe14e000     C:\Windows\system32\IMM32.DLL
0x000007feff1c0000 - 0x000007feff2c9000     C:\Windows\system32\MSCTF.dll
0x00000000577a0000 - 0x0000000057871000     C:\Program Files\Java\jre1.8.0_102\bin\msvcr100.dll
0x0000000054d50000 - 0x00000000555ea000     C:\Program Files\Java\jre1.8.0_102\bin\server\jvm.dll
0x000007fef5420000 - 0x000007fef5429000     C:\Windows\system32\WSOCK32.dll
0x000007fefdf90000 - 0x000007fefdfdd000     C:\Windows\system32\WS2_32.dll
0x000007fefdd80000 - 0x000007fefdd88000     C:\Windows\system32\NSI.dll
0x000007fefa1d0000 - 0x000007fefa20b000     C:\Windows\system32\WINMM.dll
0x000007fefc700000 - 0x000007fefc70c000     C:\Windows\system32\VERSION.dll
0x0000000077ac0000 - 0x0000000077ac7000     C:\Windows\system32\PSAPI.DLL
0x0000000064bc0000 - 0x0000000064bcf000     C:\Program Files\Java\jre1.8.0_102\bin\verify.dll
0x00000000598e0000 - 0x0000000059909000     C:\Program Files\Java\jre1.8.0_102\bin\java.dll
0x000000005f250000 - 0x000000005f266000     C:\Program Files\Java\jre1.8.0_102\bin\zip.dll
0x000007fefe150000 - 0x000007fefeeda000     C:\Windows\system32\SHELL32.dll
0x000007feffa00000 - 0x000007feffc03000     C:\Windows\system32\ole32.dll
0x000007fefd630000 - 0x000007fefd63f000     C:\Windows\system32\profapi.dll
0x0000000491120000 - 0x0000000491138000     C:\Users\GBarbieri\Documents\NetBeansProjects\jni\hello.dll
0x0000000180040000 - 0x0000000180630000     C:\cygwin64\bin\cygwin1.dll
0x000007fef16f0000 - 0x000007fef1815000     C:\Windows\system32\dbghelp.dll

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

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_102
PATH=C:\cygwin64\usr\local\bin;C:\cygwin64\bin;C:\ProgramData\Oracle\Java\javapath;C:\jet11.0-eval-amd64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;cygwin-directory\bin;C:\Program Files (x86)\CMake\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Microsoft\Web Platform Installer;C:\Program Files (x86)\Skype\Phone;C:\Program Files\Microsoft SQL Server\110\Tools\Binn;C:\Users\GBarbieri\Documents\NetBeansProjects\nifty-gui\nifty-examples\target\natives;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0;C:\Program Files\Microsoft SQL Server\120\Tools\Binn;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_102\bin
USERNAME=GBarbieri
SHELL=/bin/bash
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 60 Stepping 3, GenuineIntel



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

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

CPU:total 8 (4 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, rtm, lzcnt, ht, tsc, tscinvbit, bmi1, bmi2

Memory: 4k page, physical 16694948k(12736124k free), swap 33388036k(28626128k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.102-b14) for windows-amd64 JRE (1.8.0_102-b14), built on Jun 22 2016 13:15:21 by "java_re" with MS VC++ 10.0 (VS2010)

time: Tue Oct 11 10:47:52 2016
elapsed time: 0 seconds (0d 0h 0m 0s)

Is there hope for me?

Ps: in the tutorials, he mentions gcc-4 hangs out, for me java HelloJNI hangs out. Moreover, I should have gcc5.4

My machine is: win7 x64, jdk8 102

elect
  • 6,765
  • 10
  • 53
  • 119
  • Works fine for me in the MinGW-w64 Win64 Shell on Windows 10. I compiled the C code with `gcc -Wl,--add-stdcall-alias -I/c/Program\ Files/Java/jdk1.8.0_66/include -I/c/Program\ Files/Java/jdk1.8.0_66/include/win32 -shared -o hello.dll HelloJNI.c`, then `javac HelloJNI.java` followed by `java HelloJNI`. There's a typo in your java code that I had to fix (`void private native void`), but other than that I didn't have any problems. – Michael Oct 11 '16 at 11:32
  • thanks Michael, I double check, the typo was only in this answer – elect Oct 11 '16 at 11:35
  • For me this looks like you are trying to load a 32-bit DLL from a 64-bit Java-Application. That won't work. Both, app and library, need to have the same bitness. – Simon Kraemer Oct 11 '16 at 11:37
  • 1
    cygwin has no Java, plus gcc -3 was obsoleted long ago. Stick on mingw for your experiments – matzeri Oct 11 '16 at 11:38
  • @SimonKraemer do you think the jvm crash with cygwin has to do with that? – elect Oct 11 '16 at 11:41
  • @matzeri, I'll do that, thanks for the tip, I am gonna try mingw x64 – elect Oct 11 '16 at 11:41
  • trying to install [mingw x64](https://mingw-w64.org/doku.php), it looks I have to install win-builds first, what shall I choose between Native Windows and Cygwin/MSYS [here](http://imgur.com/gvCG3u6)? – elect Oct 11 '16 at 11:57
  • I'd just install [MSYS2](http://msys2.github.io/), and then install MinGW from within MSYS2 using pacman (see [this answer](http://stackoverflow.com/a/30071634/1524450)). – Michael Oct 11 '16 at 20:18
  • Thanks for the useful links Michael, I finally got it! :) – elect Oct 12 '16 at 10:00

1 Answers1

0

It was kind of a odyssey but at the end I made it, steps:

  • Forget anything about cygwin, mingw, win-builds, download MSYS2 from here (choose 32 or 64-bit according to what version of Windows you are going to use it on)

  • After the installation, don't open directly the console, untick the option enter image description here

  • open it from start menu -> MSYS2 64bit -> MSYS2 MinGW 64-bit

  • run pacman -Sy pacman (pacman is the Arch equivalent of apt-get)

  • close the console and reopen

  • pacman -Syu

  • try first pacman -S mingw-w64-x86_64-gcc, if it fails, it is because of this bug (probably if you installed mingw32 earlier), re run adding --force, that is pacman -S --force mingw-w64-x86_64-gcc

  • generate the library gcc -Wl,--add-stdcall-alias -I"$JAVA_HOME\include\win32" -shared -o hello.dll HelloJNI.c

  • I tried to run in MSYS2, it autocompleted java command but it didnt find it (wtf?), thenI fall back using window powershell to run it:

Hello World!

Here you go...

elect
  • 6,765
  • 10
  • 53
  • 119