建议新手先找一些入门的资料参看。
以下内容中可能存在一些不正确的地方,如遇到请指出,谢谢。
[color=red]----------------------------------------------------------------SEPARATE-LINE----------------------------------------------------------------[/color]
D:\ws\JNIDemo\bin>javah -jni com.live.xscript.altas.demo.lang.jni.ShowMessage
D:\ws\JNIDemo\bin>gcc -Wall -shared MsgImpl.c -o MsgImpl.dll
jni.h: No such file or directory
设置环境变量
CPLUS_INCLUDE_PATH
C_INCLUDE_PATH
LIBRARY_PATH
http://yanchde.gozaru.jp/mingw/wingw_start.html
奇怪的人民币符号¥
http://gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html#Environment-Variables
http://www.mingw.org/wiki/IncludePathHOWTO
D:/Program Files/Java/jdk1.5.0/include/jni.h:525: error: syntax error before '*' token
[color=green]用GCC3编译一直出现如上错误,MinGW运行环境有问题?或者是缺少相关文件[/color]
In file included from MsgImpl.c:3:
com_live_xscript_altas_demo_lang_jni_ShowMessage.h:13: error: syntax error before "void"
com_live_xscript_altas_demo_lang_jni_ShowMessage.h:14: warning: data definition has no type or storage class
MsgImpl.c:5: error: syntax error before "void"
JNIEXPORT void JNICALL
Java_com_live_xscript_altas_demo_lang_jni_ShowMessage_showMessage (JNIEnv*, jobject, jstring);
将GCC升级为4.4.0以后再次编译DLL出现
D:\Program Files\Java\jdk1.5.0\include/jni.h:27:20: error: jni_md.h: No such file or directory
追加jni_md.h的路径到
C_INCLUDE_PATH
%JAVA_HOME%\include\win32
可以成功编译DLL了
MsgImpl.c:7: error: request for member 'GetStringUTFChars' in something not a st
ructure or union
C和C++的区别
注意gcc&g++
g++ -Wall -shared MsgImpl.c -o MsgImpl.dll
运行Java时出现
java.lang.UnsatisfiedLinkError
Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
找不到本地语言的定义
Exception in thread "main" java.lang.UnsatisfiedLinkError: showMessage
at com.live.xscript.altas.demo.lang.jni.ShowMessage.showMessage(Native Method)
at com.live.xscript.altas.demo.lang.jni.ShowMessage.main(ShowMessage.java:16)
[color=green]
网上比较多的这种错误是找不到DLL
但这里能找到DLL,找不到方法[/color]
迷茫了很久,查了很久,发现了[url=http://dikar.iteye.com/blog/382701]这篇[/url]http://dikar.iteye.com/blog/382701文章
用Dependency Walker查看生成的DLL
方法名字为Java_com_live_xscript_altas_demo_lang_jni_ShowMessage_showMessage[color=blue]@12[/color]
比起正规的名字多了'[color=blue]@12[/color]'
http://dikar.iteye.com/blog/382701
提供了解决办法
g++ -Wall -Wl,--kill-at -shared ShowMessage.c -o ShowMessage.dll
或者
g++ -Wall -Wl,--add-stdcall-alias -shared ShowMessage.c -o ShowMessage.dll
于是最简单的JNI程序就出来了,只有一个Java文件ShowMessage
后面的另一个程序,往Native程序中传Java对象就相对简单多了
MyJavaClass.java
UserObjects.java
两个文件
ld --help
ld是干什么的
还有很多问题没有解决
native方法返回值问题
GetStringUTFChars等方法的返回值类型,只能是char吗?
下面是GCC环境变量
g++ --version
g++ (GCC) 4.4.0
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MinGW
D:\Program Files\ccpp\MinGW
C_INCLUDE_PATH
%JAVA_HOME%\include;%JAVA_HOME%\include\win32;%MINGW%\include;%MINGW%\lib\gcc\mingw32\4.4.0\include;%WIN32API_BASE%\include
CPLUS_INCLUDE_PATH
%MinGW%\lib\gcc\mingw32\4.4.0\include\c++;%MinGW%\lib\gcc\mingw32\4.4.0\include\c++\backward;%C_INCLUDE_PATH%
LIBRARY_PATH
%JAVA_HOME%\lib;%MINGW%\lib;%MinGW%\lib\gcc\mingw32\4.4.0;%WIN32API_BASE%\lib
PATH
%MinGW%\bin;%MinGW%\libexec\gcc\mingw32\4.4.0;%PYTHON_HOME%;%JAVA_HOME%\bin;d:\oracle\product\10.2.0\db_1\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;D:\Program Files\TortoiseSVN\bin;D:\Program Files\md5;%GNU_BASE%\make\bin
问题&解决方法
d:/program files/ccpp/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -luser32
collect2: ld returned 1 exit status
追加
WIN32API_BASE
D:\Program Files\ccpp\w32api-3.13
缺少相关的lib
libgcc_s_dw2-1.dll在gcc-core-4.4.0-mingw32-dll中
在gcc4.4.0下载页面中(http://sourceforge.net/projects/mingw/files/)
gmp
mpfr