《一线大厂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文件绝对路径
最后
由于篇幅限制,小编在此截出几张知识讲解的图解
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
g-bDslyAiE-1714655231643)]
[外链图片转存中…(img-Wm6VZaNs-1714655231644)]
[外链图片转存中…(img-WMcv0nao-1714655231644)]
[外链图片转存中…(img-pxCTkJmZ-1714655231644)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!