不知道具体原理,但是,加载 dll 文件时,带路径或者更改 dll 文件的名字,都会报错。虽然库记载成功了,但是处女座认为这不可接受。于是有了这个解决方案。
在根目录为库创建软连接,然后使用 system.loadLibrary(“libname”) 来加载。事实证明,它是认软连接的。
上代码:
import com.seapine.surroundscm.api.*;
import java.lang.UnsupportedOperationException;
import java.lang.SecurityException;
import java.io.IOException;
import java.nio.file.*;
public class SurroundSCMAPIExample {
static {
String arch = System.getProperty("sun.arch.data.model");
Path target;
if(arch.equals("32"))
{
target = FileSystems.getDefault().getPath("lib","sscmapi.dll");
}
else
{
target = FileSystems.getDefault().getPath("lib64","sscmapi.dll");
}
Path link = FileSystems.getDefault().getPath("sscmapi.dll");
try {
Files.deleteIfExists(link);
Files.createSymbolicLink(link, target);
} catch (IOException | UnsupportedOperationException | SecurityException e) {
if (e instanceof SecurityException) {
System.err.println("Permission denied!");
}
if (e instanceof UnsupportedOperationException) {
System.err.println("An unsupported operation was detected!");
}
if (e instanceof IOException) {
System.err.println("An I/O error occurred!");
}
System.err.println(e);
}
System.loadLibrary("sscmapi");
}
public static void main(String[] args)
{
SSCMContext context = new SSCMContext();
SSCMResult ret = SSCMAPI.connectRSA("localhost",
4900,
"",
"admin",
"",
context);
if(SSCMAPI.SSCM_API_OK == ret.result)
System.out.println("Connection Succeeded");
else
System.out.println("Connection Failed: " +
SSCMAPI.getLastError(ret.result));
SSCMAPI.disconnect(context);
}
}
妥妥的,再看不到任何的报错。
不过这个方案有个坑,如果软链接文件是 read-only 的,创建软链接就有麻烦了。所以,在打包,或者是从仓库 get 时,要注意删掉软链接。
——————
无论在哪里做什么,只要坚持服务、创新、创造价值,其他的东西自然都会来的。
Original: https://www.cnblogs.com/pied/p/11133413.html
Author: Biiigfish
Title: Java 加载动态库 dll 文件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/537549/
转载文章受原作者版权保护。转载请注明原作者出处!