springboot调用so文件

话不多说,直接进入重点。

1. 导入jna依赖
        <dependency>
            <groupId>com.sun.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.0.9</version>
        </dependency>
2. 在resources下新建目录linux-x86-64,并在这个目录中放入so文件

在这里插入图片描述

3. 调用如下初始化代码,多个so调用时请自行封装
static {
	try {
		String path = System.getProperty("java.io.tmpdir");
		String name = "libmath.so";
		// 获取sources下的资源
		ClassPathResource classPathResource = new ClassPathResource("linux-x86-64/" + name);
		InputStream in = classPathResource.getInputStream();
		// 写入到临时文件
		FileUtil.writeFile(path + "/lib/" + name, in);
	 } catch (IOException e) {
	     // 
	 }
}
4. 建立Math.class映射
package com.xxx.jni;

import com.sun.jna.Library;
import com.sun.jna.Structure;

public interface Math extends Library {
    
    int max(int a, int b);

    class TestStruct extends Structure {
		
        public static class ByReference extends TestStruct implements Structure.ByReference {
        }

        public static class ByValue extends TestStruct implements Structure.ByValue {
        }
    }
}

调用:
    @PostMapping("/A0018")
    public ServerResponse A0018() {
    	String path = System.getProperty("java.io.tmpdir");
    	Math mathJNI = (Math) Native.loadLibrary(path + "/lib/" + name, Math.class);
        int max = mathJNI.max(5432, 211233);
        return ServerResponse.success(max);
    }

结果:

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值