linux操作系统下 实现java调用so文件

最近公司在做农机自动驾驶业务,里面的有些算法是c++代码实现,需要对接到java平台。因此我们可以用强大的jna很简单的就可以实现调用so文件库。

在linux下使用jna调用本地方法的时候,需要将C写成的程序编译为so文件。

1.首先编写一个test.c文件


  
  1. #include<stdio.h>
  2. int add(int a,int b);
  3. int add(int a,int b)
  4. {
  5. int c = a + b ;
  6. return c ;
  7. }

2.确保已经安装gcc,利用gcc命令编译为动态链接库


  
  1. gcc -fpic -c test.c
  2. gcc -shared -o libtest.so test.o
  3. ls看一下生成的文件:
  4. libtest.so test.c test.o

这样就会生成so文件了。

注意:这里为什么要命名为libtest.so呢?因为jna在找so文件的时候,要匹配前缀为lib 的so文件,在linux系统中是这样的,但是在windows系统就要命名为test.dll,jna找dll文件时候,直接匹配test就可以。

3、下载jna-4.2.2.jar,设置环境变量


4.编写TestSo.java文件


  
  1. import com.sun.jna.Library;
  2. import com.sun.jna.Native;
  3. public class TestSo {
  4. public interface LgetLib extends Library {
  5. // 调用linux下面的so文件,注意,这里只要写test就可以了,不要写libtest,也不要加后缀
  6. LgetLib INSTANCE = (LgetLib) Native.loadLibrary("test",LgetLib.class);
  7. int add(int a,int b);
  8. }
  9. public int add(int a,int b){
  10. return LgetLib.INSTANCE.add(a,b);
  11. }
  12. public static void main(String[] args) {
  13. TestSo ts = new TestSo();
  14. int c = ts.add(10,20);
  15. System.out.println("10+20="+c);
  16. }
  17. }

5.编译运行java文件


  
  1. javac TestSo.java
  2. java TestSo

  
  1. <strong style="color: black; background-color: rgb(255, 255, 102);">java</strong> TestSo
  2. Exception in thread "main" <strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>.lang.UnsatisfiedLinkError: Unable to load library 'test': libtest.so: cannot open shared object file: No such file or directory
  3. at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:163)
  4. at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:236)
  5. at com.sun.jna.Library$Handler.<init>(Library.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:140)
  6. at com.sun.jna.Native.loadLibrary(Native.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:379)
  7. at com.sun.jna.Native.loadLibrary(Native.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:364)
  8. at TestSo$LgetLib.<clinit>(TestSo.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:7)
  9. at TestSo.add(TestSo.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:11)
  10. at TestSo.main(TestSo.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:15)

这个错误是指找不到so文件,于是我们将so文件所在的目录设置到环境变量LD_LIBRARY_PATH中:

vim /etc/profile

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${你的so文件目录}

source /etc/profile 

之后,你可以检查一下设置的起没起效果:echo $LD_LIBRARY_PATH,如果出现你设置的内容就对了,如果没有,你可以重新打开一个窗口再查一下

设置好环境变量之后,你就可以运行java类了:

java TestSo
10+20=30

到此运行成功。

如果你遇到了紧急的情事,而这个地方又搞不定,你可以暂时交so文件放到/usr/lib或者usr/lib64这个目录下面,一般是可以使程序运行的。

6.另外附上 查看so文件库的linux命令

1、nm -D xxx.so

2、readelf -s xxx.so

3、objump -tT xxx.so

7.常见错误

1.声明函数一定要是 extern “C” __declspec(dllexport) 不然会报错 Exception in thread “main” java.lang.UnsatisfiedLinkError: Error looking up function ‘Preprocess’: 找不到指定的程序。

2、Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/zhangyg/clibs/librdp.so: /home/zhangyg/clibs/libXXX.so: undefined symbol: SHA1_Init

连接动态库时加上-lssl,如下:

ld -shared -lssl $(OBJECTS) -o $(SHARED_LIB)

3、Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/zhangyg/clibs/librdp.so: /home/zhangyg/clibs/libXXX.so: undefined symbol: stat

生成动态库时不用ld,而用gcc,如下

gcc  -shared -lssl $(OBJECTS) -o $(SHARED_LIB)




文章来源: blog.csdn.net,作者:血煞风雨城2018,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_31905135/article/details/80756057

(完)