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

举报
小米粒-biubiubiu 发表于 2020/11/30 23:21:48 2020/11/30
【摘要】 最近公司在做农机自动驾驶业务,里面的有些算法是c++代码实现,需要对接到java平台。因此我们可以用强大的jna很简单的就可以实现调用so文件库。 在linux下使用jna调用本地方法的时候,需要将C写成的程序编译为so文件。 1.首先编写一个test.c文件 #include<stdio.h> int add(int a,int b); int add(int a,in...

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

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

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


      #include<stdio.h>
      int add(int a,int b);
      int add(int a,int b)
      {
       int c = a + b ;
       return c ;
      }
  
 

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


      gcc -fpic -c test.c
      gcc -shared -o libtest.so test.o
      ls看一下生成的文件:
      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文件


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

5.编译运行java文件


      javac TestSo.java
      java TestSo
  
 

      <strong style="color: black; background-color: rgb(255, 255, 102);">java</strong> TestSo
      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
      at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:163)
      at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:236)
      at com.sun.jna.Library$Handler.<init>(Library.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:140)
      at com.sun.jna.Native.loadLibrary(Native.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:379)
      at com.sun.jna.Native.loadLibrary(Native.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:364)
      at TestSo$LgetLib.<clinit>(TestSo.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:7)
      at TestSo.add(TestSo.<strong style="color: black; background-color: rgb(255, 255, 102);">java</strong>:11)
        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

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。