#java #c #java-native-interface #cygwin #mingw
#java #c #java-native-interface #cygwin #mingw
Вопрос:
Я пытаюсь заставить работать самый простой пример jni.
Я следую этому руководству, немного старому, но лучшему, что я смог найти
Шаги:
-
создайте
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 } }
-
Скомпилируйте
HelloJNI.java
вHelloJNI.class
:javac HelloJNI.java
-
Создайте заголовок, выполнив:
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
-
Напишите
HelloJNI.c
файл:#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; }
А теперь начинается самое интересное…
Прежде всего, %JAVA_HOME%
не работает, вы должны использовать $JAVA_HOME
set JAVA_HOME=C:Program FilesJavajdk1.8.0_102
echo $JAVA_HOME
gcc -Wl,--add-stdcall-alias -I"$JAVA_HOMEinclude" -I"$JAVA_HOMEincludewin32" -shared -o hello.dll HelloJNI.c
Похоже, я это сделал… ehm нет:
Затем я попробовал с Cygwin:
gcc-3 -D __int64="long long" -mno-cygwin -Wl,--add-stdcall-alias -I"<JAVA_HOME>include" -I"<JAVA_HOME>includewin32" -shared -o hello.dll HelloJNI.c
-bash: gcc-3: команда не найдена
Позвольте мне изменить gcc-3
на gcc
:
gcc -D __int64="long long" -mno-cygwin -Wl,--add-stdcall-alias -I"<JAVA_HOME>include" -I"<JAVA_HOME>includewin32" -shared -o hello.dll HelloJNI.c
gcc: ошибка: неизвестен параметр -mno-cygwin
Давайте попробуем это устранить
gcc -D __int64="long long" -Wl,--add-stdcall-alias -I"<JAVA_HOME>include" -I"<JAVA_HOME>includewin32" -shared -o hello.dll HelloJNI.c
нет такого файла «jni.h»
позвольте мне изменить на $JAVA_HOME
gcc -D __int64="long long" -Wl,--add-stdcall-alias -I"$JAVA_HOMEinclude" -I"$JAVA_HOMEincludewin32" -shared -o hello.dll HelloJNI.c
возможно, на этот раз это сработало..
java HelloJNI
зависает
java -Djava.library.path=. HelloJNI
ошибка сегментации
#
# 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:reworkspace8-2-build-windows-amd64-cygwinjdk8u1027268hotspotsU-¡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:reworkspace8-2-build-windows-amd64-cygwinjdk8u1027268hotspotsrcsharevmprims3ßçÄ…*¥?
Event: 0.041 Thread 0x0000000001fee800 Exception <a 'java/io/FileNotFoundException'> (0x000000076b1dc428) thrown at [C:reworkspace8-2-build-windows-amd64-cygwinjdk8u1027268hotspotsrcsharevmprimsjni.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 FilesJavajre1.8.0_102binjava.exe
0x0000000077900000 - 0x0000000077aaa000 C:WindowsSYSTEM32ntdll.dll
0x00000000776e0000 - 0x00000000777ff000 C:Windowssystem32kernel32.dll
0x000007fefd8e0000 - 0x000007fefd94a000 C:Windowssystem32KERNELBASE.dll
0x000007fefda70000 - 0x000007fefdb4b000 C:Windowssystem32ADVAPI32.dll
0x000007fefdfe0000 - 0x000007fefe07f000 C:Windowssystem32msvcrt.dll
0x000007feff860000 - 0x000007feff87f000 C:WindowsSYSTEM32sechost.dll
0x000007fefdd90000 - 0x000007fefdebd000 C:Windowssystem32RPCRT4.dll
0x0000000077800000 - 0x00000000778fa000 C:Windowssystem32USER32.dll
0x000007feff8e0000 - 0x000007feff947000 C:Windowssystem32GDI32.dll
0x000007feff950000 - 0x000007feff95e000 C:Windowssystem32LPK.dll
0x000007fefdec0000 - 0x000007fefdf8a000 C:Windowssystem32USP10.dll
0x000007fefbf90000 - 0x000007fefc184000 C:WindowsWinSxSamd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.18837_none_fa3b1e3d17594757COMCTL32.dll
0x000007fefe0a0000 - 0x000007fefe111000 C:Windowssystem32SHLWAPI.dll
0x000007fefe120000 - 0x000007fefe14e000 C:Windowssystem32IMM32.DLL
0x000007feff1c0000 - 0x000007feff2c9000 C:Windowssystem32MSCTF.dll
0x00000000577a0000 - 0x0000000057871000 C:Program FilesJavajre1.8.0_102binmsvcr100.dll
0x0000000054d50000 - 0x00000000555ea000 C:Program FilesJavajre1.8.0_102binserverjvm.dll
0x000007fef5420000 - 0x000007fef5429000 C:Windowssystem32WSOCK32.dll
0x000007fefdf90000 - 0x000007fefdfdd000 C:Windowssystem32WS2_32.dll
0x000007fefdd80000 - 0x000007fefdd88000 C:Windowssystem32NSI.dll
0x000007fefa1d0000 - 0x000007fefa20b000 C:Windowssystem32WINMM.dll
0x000007fefc700000 - 0x000007fefc70c000 C:Windowssystem32VERSION.dll
0x0000000077ac0000 - 0x0000000077ac7000 C:Windowssystem32PSAPI.DLL
0x0000000064bc0000 - 0x0000000064bcf000 C:Program FilesJavajre1.8.0_102binverify.dll
0x00000000598e0000 - 0x0000000059909000 C:Program FilesJavajre1.8.0_102binjava.dll
0x000000005f250000 - 0x000000005f266000 C:Program FilesJavajre1.8.0_102binzip.dll
0x000007fefe150000 - 0x000007fefeeda000 C:Windowssystem32SHELL32.dll
0x000007feffa00000 - 0x000007feffc03000 C:Windowssystem32ole32.dll
0x000007fefd630000 - 0x000007fefd63f000 C:Windowssystem32profapi.dll
0x0000000491120000 - 0x0000000491138000 C:UsersGBarbieriDocumentsNetBeansProjectsjnihello.dll
0x0000000180040000 - 0x0000000180630000 C:cygwin64bincygwin1.dll
0x000007fef16f0000 - 0x000007fef1815000 C:Windowssystem32dbghelp.dll
VM Arguments:
java_command: HelloJNI
java_class_path (initial): .
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:Program FilesJavajdk1.8.0_102
PATH=C:cygwin64usrlocalbin;C:cygwin64bin;C:ProgramDataOracleJavajavapath;C:jet11.0-eval-amd64bin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;cygwin-directorybin;C:Program Files (x86)CMakebin;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program FilesMicrosoftWeb Platform Installer;C:Program Files (x86)SkypePhone;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:UsersGBarbieriDocumentsNetBeansProjectsnifty-guinifty-examplestargetnatives;C:Program Files (x86)Microsoft SDKsTypeScript1.0;C:Program FilesMicrosoft SQL Server120ToolsBinn;C:Program Files (x86)Windows Kits10Windows Performance Toolkit;C:Program FilesGitcmd;C:Program FilesJavajdk1.8.0_102bin
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)
Есть ли у меня надежда?
Ps: в уроках он упоминает gcc-4
, что зависает, для меня java HelloJNI
зависает. Более того, я должен был gcc5.4
Моя машина: win7 x64, jdk8 102
Комментарии:
1. У меня отлично работает в оболочке MinGW-w64 Win64 в Windows 10. Я скомпилировал код C с
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
помощью, затемjavac HelloJNI.java
последовалjava HelloJNI
. В вашем java-коде есть опечатка, которую мне пришлось исправить (void private native void
) , но кроме этого у меня не было никаких проблем.2. спасибо, Майкл, я перепроверяю, опечатка была только в этом ответе
3. Для меня это выглядит так, как будто вы пытаетесь загрузить 32-разрядную DLL из 64-разрядного Java-приложения. Это не сработает. Оба, приложение и библиотека, должны иметь одинаковую разрядность.
4. в cygwin нет Java, плюс gcc -3 давно устарел. Придерживайтесь mingw для своих экспериментов
5. @SimonKraemer как вы думаете, сбой jvm с cygwin связан с этим?
Ответ №1:
Это была своего рода одиссея, но в конце я сделал это, шаги:
-
Забудьте что-либо о cygwin, mingw, win-сборках, загрузите MSYS2 отсюда (выберите 32 или 64-разрядную версию в зависимости от версии Windows, в которой вы собираетесь ее использовать)
-
После установки не открывайте консоль напрямую, снимите флажок с опции
-
откройте его из меню «Пуск» -> MSYS2 64bit -> MSYS2 MinGW 64-разрядный
-
выполнить
pacman -Sy pacman
(pacman — это Arch-эквивалент apt-get) -
закройте консоль и снова откройте
-
pacman -Syu
-
сначала попробуйте
pacman -S mingw-w64-x86_64-gcc
, если это не удается, это из-за этой ошибки (возможно, если вы установили mingw32 ранее), повторно запустите добавление--force
, то естьpacman -S --force mingw-w64-x86_64-gcc
-
создайте библиотеку
gcc -Wl,--add-stdcall-alias -I"$JAVA_HOMEincludewin32" -shared -o hello.dll HelloJNI.c
-
Я попытался запустить в MSYS2, он автоматически
java
выполнил команду, но не нашел ее (wtf?), Затем я вернулся к использованию window powershell для ее запуска:
Привет, мир!
Вот и все…