0

我正在尝试从 jar 中加载一个 dll 文件,以使用带有此修复程序的小程序:

从 JAR 中提取和加载 DLL

我创建文件,将其设置为可写和可执行,但是当我使用 canWrite()/canExecute() 检查它时,它返回 false。小程序已签名。

编码:

private static void loadLib() {
URL res = SystemActivityNotifications.class.getResource("/sys-native
    /sysactivitynotifications.dll");
InputStream in = res.openStream();
File dll = new File(path + "sysactivitynotifications.dll");
dll.setExecutable(true);
dll.setWritable(true);
logger.info(dll.canWrite() + " " + dll.canExecute());
FileOutputStream fos = new FileOutputStream(dll);
byte[] array = new byte[1024];
try {
    for(int i = in.read(array); i != 1; i=in.read(array)) {
        fos.write(array, 0, i);
    }
} catch (IOException e) { logger.info("Cannot write to file: " + e.getMessage()); }
fos.close();
in.close();
System.load(dll.getAbsolutePath());
}

该文件已正确创建,但在尝试写入时会引发异常。

编辑:它在我第二次运行小程序时写入文件,但如果我删除文件并再次运行,第一次迭代不起作用。

忘了说:上面的所有代码都来自 System.load.library(dll); 之后的一个 catch 块;抛出异常。

try
    {
        System.loadLibrary("sysactivitynotifications");

        ptr = allocAndInit();
        if (ptr == -1)
            ptr = 0;
    }
    catch (Throwable t)
    {
        if (t instanceof ThreadDeath)
            throw (ThreadDeath) t;
        else {
           loadLib();
        }
    }

编辑:它向我抛出了这个错误:

java.lang.UnsatisfiedLinkError: C:\: The process cannot access the file because it is being used by another process
4

1 回答 1

1

..尝试从 jar 加载 dll 文件以使用小程序..

对于部署 Java 小程序,最好的选择通常是使用Java Web Start 启动小程序。JWS 适用于 Windows、OS X 和 *nix。

Java Web Start (JWS) 是 Oracle Corporation 用于直接从网络或 Internet 链接启动富客户端(Swing、Java-FX、AWT、SWT..)桌面应用程序的技术。它为支持 Java 的平台提供“一键式”安装。

JWS 提供了许多吸引人的功能,包括但不限于启动屏幕、桌面集成、文件关联、自动更新(包括延迟下载和更新的编程控制)、按平台、架构或区域设置本地和其他资源下载的分区、配置运行时环境(最低 J2SE 版本、运行时选项、RAM 等),使用扩展轻松管理公共资源。

将本机放在(已签名)Jar 的根目录中,并将对该 Jar 的引用放在启动文件中,它们将被放置在应用程序的类路径中。

于 2013-10-22T12:09:49.527 回答