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

最近公司在做农机自动驾驶业务,里面的有些算法是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/qq_31905135/article/details/80756057