关于java jni调用c++动态库的一些问题及解决方法

最近使用java jni接口技术调用c++完成的动态库,平台为虚拟机下的centos 6.6。

编译出来的*.so 文件在被java调用过程中出现了各种错误。

1.

java com.cmsz.znw.filevalmain.FileValServerImpl
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/xxx/Desktop/test/libZnwFileVal.so: com_cmsz_znw_filevalmain_FileValServerImpl.o: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at com.cmsz.znw.filevalmain.FileValServerImpl.<clinit>(FileValServerImpl.java:12)


2.

OpenJDK 64-Bit Server VM warning: You have loaded library /home/xxx/Desktop/test/libZnwFileVal.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: /home/xxx/Desktop/test/libZnwFileVal.so: /home/xxx/Desktop/test/libZnwFileVal.so: invalid ELF header (Possible cause: endianness mismatch)
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at com.cmsz.znw.filevalmain.FileValServerImpl.<clinit>(FileValServerImpl.java:12)


3.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f5c8baed5c6, pid=11530, tid=0x00007f5c8b47c700
#
# JRE version: OpenJDK Runtime Environment (8.0_111-b15) (build 1.8.0_111-b15)
# Java VM: OpenJDK 64-Bit Server VM (25.111-b15 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x6705c6]
#
# Core dump written. Default location: /home/ylwei/Desktop/test/core or core.11530
#
# An error report file with more information is saved as:
# /home/xxx/Desktop/test/hs_err_pid11530.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
Aborted (core dumped)


从网上搜了各种办法,没有能解决问题。怀疑有可能是动态库编译时候选项有问题。也有可能java文件有问题。


///

20170120更新

原来是编译过程中依赖的静态库需要重新编译一下才能正常使用。拷贝过来的静态库是在其他虚拟机上编译的,而我的gcc版本为6.1.0,他的gcc版本为4.8.5。

将所有用到的静态库重新编译了一遍,确实可用。

猜你喜欢

转载自blog.csdn.net/u011803182/article/details/53841138