spring boot jar包启动JNI相对路径加载dll方案二

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import java.io.File;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.util.Iterator;

import java.util.Vector;

@Component

@Slf4j

public class JniUtil {

@PostConstruct

public static void loadDll_1() {

loadDLL(“dll_1”, “auoLoad.dll”);

}

public static void loadDll_2() {

loadDLL(“dll_2”, “projectForJava.dll”);

}

public static void unloadDll_1() { unloadDLL(“auoLoad.dll”); }

public static void unloadDll_2() {

unloadDLL(“projectForJava.dll”);

}

private synchronized static void loadDLL(String folderName, String libName) {

String classPath = new JniUtil().getClass().getResource(“/”).getPath();

if (classPath.indexOf(“.jar”) > 0) {

classPath = classPath.substring(0, classPath.lastIndexOf(“.jar”));

classPath = classPath.substring(6, classPath.lastIndexOf(“/”));

String dllPath = File.separator + classPath + “/” + folderName;

log.info(“dll加载路径:" + dllPath + "”);

System.load(dllPath + “/” + libName);

} else {

String projectPath = classPath.replace(“target/classes/”, “”);

String dllPath = projectPath + folderName + “/” + libName;

log.info(“dll加载路径:" + dllPath + "”);

}

log.info(“dll加载成功”);

}

public synchronized static void unloadDLL(String libName) {

try {

ClassLoader classLoader = JniUtil.class.getClassLoader();

Field field = ClassLoader.class.getDeclaredField(“nativeLibraries”);

field.setAccessible(true);

Vector libs = (Vector) field.get(classLoader);

Iterator it = libs.iterator();

while (it.hasNext()) {

Object object = it.next();

Field[] fs = object.getClass().getDeclaredFields();

for (int k = 0; k < fs.length; k++) {

if (fs[k].getName().equals(“name”)) {

fs[k].setAccessible(true);

String dllPath = fs[k].get(object).toString();

if (dllPath.endsWith(libName)) {

Method finalize = object.getClass()

.getDeclaredMethod(“finalize”);

finalize.setAccessible(true);

finalize.invoke(object);

}

}

}

}

log.info(“dll卸载成功”);

} catch (Throwable th) {

log.error(“dll卸载失败”);

th.printStackTrace();

}

}

}

jni接口代码如下

package com.hxtx.stripmine.jni;

import org.springframework.stereotype.Component;

/**

  • JNI调用dll方法

  • @since JDK1.8

  • @author tarzan Liu

  • @date 2021年04月01日 14:37:43

*/

@Component

public class GraphicJNIService {

/**

  • 方法描述: 解析dem材质边界

  • @param fileUrl (dwg文件地址)

  • @Return {@link String}

  • @author tarzan Liu

  • @date 2021年04月01日 14:38:05

*/

public native String parseLayer(String fileUrl);

/**

  • 方法描述: dwg文件编译1

  • @param tempFolder 中间文件文件夹绝对路径

  • @param dwgFolder 所需材料文件夹绝对路径

  • @param materialInfo 当前启用的地形材质配置信息

  • @param netWorkJsonPath 生成的路网json文件绝对路径

最后

由于篇幅限制,小编在此截出几张知识讲解的图解

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
g-bDslyAiE-1714655231643)]

[外链图片转存中…(img-Wm6VZaNs-1714655231644)]

[外链图片转存中…(img-WMcv0nao-1714655231644)]

[外链图片转存中…(img-pxCTkJmZ-1714655231644)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值