1. 程式人生 > >關於java jni呼叫c++動態庫的一些問題及解決方法

關於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。

將所有用到的靜態庫重新編譯了一遍,確實可用。