Part 1
Ubuntu下JNI的简单使用:
http://blog.csdn.net/fengqiaoyebo2008/article/details/6210499
Part 2
在eclipse中快速开发JNI,一键生成C头文件.h,以及一键使用NDK交叉编译:
http://www.oschina.net/question/1402563_133543
Part 3
JNI java.lang.UnsatisfiedLinkError解决方法:
http://dikar.iteye.com/blog/382701
JNI java.lang.UnsatisfiedLinkError解决方法:
http://www.360doc.com/content/09/0402/09/107226_2994393.shtml
Java调用本地C/C++动态库拾遗 JNI/JNA与名称粉碎:
http://blog.sina.com.cn/s/blog_3f2c72ea01011kvx.html
JNI调用本地方法 java.lang.UnsatisfiedLinkError: XXXclass.XXXmethod()异常:
http://www.cnblogs.com/javawebsoa/archive/2013/07/30/3225942.html
生成头文件的shell: hadoop@Node4:/usr/local/eclipse/workspace/MongoLoc/bin$ javah -jni tong.mongo.loction.LinkedC 编 译C程序的shell: g++ -shared -I /usr/local/bin/jdk1.7.0_71/include -I /usr/local/bin/jdk1.7.0_71/include/linux main.cpp -o libSpeedEstimate.so |
0. 错误 :
java.lang.UnsatisfiedLinkError: XXXclass.XXXmethod()
解决:
之前在windows系统下运行需要在本地方法的头文件中中的函数前面加上下划线,比如以前是 JNIEXPORT void JNICALL Java_TestNative_Hello (JNIEnv * , jobject ); 现在改成 JNIEXPORT void JNICALL _Java_TestNative_Hello (JNIEnv * , jobject ); 同时你的实现的cpp文件或者c文件里的函数头也要一致 前面有下划线。
但是移植到ubuntu下,出现此错误。去掉加的下划线后又可以运行。
1.错误:
运行程序,在eclipse控制台出现
Java HotSpot(TM) Server VM warning: You have loaded library /usr/local/eclipse/workspace/MongoLoc/libSpeedEstimate.so which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/local/eclipse/workspace/MongoLoc/libSpeedEstimate.so: /usr/local/eclipse/workspace/MongoLoc/libSpeedEstimate.so: 错误 ELF 类: ELFCLASS64 (Possible cause: architecture word width mismatch)
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at tong.mongo.loction.LinkedC.<clinit>(LinkedC.java:10)
at tong.mongo.loction.MdbFind.main(MdbFind.java:173)
原因:
原因是编译使用的jvm是64位的,我们的系统jVM是32位的版本。c程序在64位系统下编译,不能在32位下用。
解决:
c程序重新在32位系统编译
2. 错误:
在命令窗口输入 gcc -shared -I /usr/local/bin/jdk1.7.0_71/include/ -I /usr/local/bin/jdk1.7.0_71/include/linux/ main.cpp -o libSpeedEstimate.so
进行编译
gcc: error trying to exec 'cc1plus': execvp: 没有那个文件或目录
原因:
gcc 跟 g++ 版本不一致,或者没有安装g++
解决:
$sudo apt-get install g++
3. 错误:运行程序出现 libSpeedEstimate.so: undefined symbol: _ZNSt8ios_base4InitC1Ev
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/local/eclipse/workspace/MongoLoc/libSpeedEstimate.so: /usr/local/eclipse/workspace/MongoLoc/libSpeedEstimate.so: undefined symbol: _ZNSt8ios_base4InitC1Ev
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at tong.mongo.loction.LinkedC.<clinit>(LinkedC.java:10)
at tong.mongo.loction.MdbFind.main(MdbFind.java:173)
原因:
译有问题,用g++ 编译可以,可能是有个cpp文件,gcc虽然能编译,但是生成的so文件格式有些问题,jni找不到接口,用g++就可以
原来的命令:
gcc -shared -I /usr/local/bin/jdk1.7.0_71/include/ -I /usr/local/bin/jdk1.7.0_71/include/linux/ main.cpp -o libSpeedEstimate.so
修改:
g++ -shared -I /usr/local/bin/jdk1.7.0_71/include/ -I /usr/local/bin/jdk1.7.0_71/include/linux/ main.cpp -o libSpeedEstimate.so
然后重新运行程序
见连接:http://bbs.csdn.net/topics/290013271