dll动态库 java,Java 加载动态库 dll 文件

这篇博客介绍了一种在Java中加载DLL库时避免错误的方法,通过创建软链接来解决路径或文件名更改导致的加载失败问题。示例代码展示了如何根据系统架构动态创建软链接,并使用System.loadLibrary()加载库。
摘要由CSDN通过智能技术生成

不知道具体原理,但是,加载 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);

}

}

妥妥的,再看不到任何的报错。

标签:Java,java,System,dll,sscmapi,println,import,加载

来源: https://www.cnblogs.com/pied/p/11133413.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值