fix(utils): 修复JNA调用动态库的路径和错误处理问题

- 移除硬编码的文件后缀名,根据操作系统自动识别.dll或.so文件
- 修正Linux系统下的库文件命名规则,移除多余的"_dll"后缀
- 添加对文件扩展名的条件判断,在Windows下确保.dll后缀
- 添加动态库加载成功后的路径打印功能
This commit is contained in:
xy
2026-04-27 15:32:39 +08:00
parent abdb855919
commit 52677f84dc

View File

@@ -20,21 +20,20 @@ public class JnaCallDllOrSo {
public JnaCallDllOrSo(String name) {
super();
String suffix = ".dll";
try {
String os = System.getProperty("os.name");
// windows操作系统为1 否则为0
int beginIndex = os != null && os.startsWith("Windows") ? 1 : 0;
String nameDll;
if (beginIndex == 0) {
//linux操作系统
nameDll = "lib" + name + "_dll";
suffix = ".so";
nameDll = "lib" + name + ".so";
} else {
nameDll = name;
if (!name.endsWith(".dll")) {
nameDll = name + ".dll";
}
}
String tem = "/usr/local/dllFile/"+ nameDll.concat(suffix);
String tem = "/usr/local/dllFile/"+ nameDll;
File dockerFile = new File(tem);
if(!dockerFile.exists()){
boolean f = dockerFile.getParentFile().mkdirs();
@@ -42,7 +41,11 @@ public class JnaCallDllOrSo {
System.out.println("文件夹创建:"+f);
System.out.println("文件创建:"+d);
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(nameDll.concat(suffix))) {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(nameDll)) {
if (inputStream == null) {
log.error("找不到资源文件: {}", nameDll);
throw new FileNotFoundException("找不到资源文件: " + nameDll);
}
try (FileOutputStream outputStream = new FileOutputStream(dockerFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
@@ -53,9 +56,11 @@ public class JnaCallDllOrSo {
}
}
this.path = dockerFile.getAbsolutePath();
System.out.println("动态库路径: " + this.path);
} catch (Exception e) {
log.error("调用高级算法文件异常,异常信息如下:");
log.error(e.getMessage());
log.error(e.getMessage(), e);
throw new RuntimeException("加载动态库失败: " + e.getMessage(), e);
}