You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JNA加载opus原生API,解决Maven多模块下opus.dll加载失败问题

解决方案

1. 修正资源路径与文件名

你当前的pom资源配置存在两个核心问题:

  • JNA默认会在类路径下的win32-x86-64目录下查找opus.dll,你当前存放的dll名为libopus.dll,路径为natives目录,和JNA的默认扫描规则不匹配
  • maven的targetPath配置是相对于jar包根目录的,你配置的./src/main/resources/natives会导致dll被打包到jar包内部的src/main/resources/natives路径下,不会被JNA扫描到

操作步骤:

  • 将本地的libopus.dll重命名为opus.dll
  • 在sound模块的src/main/resources下新建win32-x86-64文件夹,把重命名后的opus.dll放入该文件夹
  • 替换pom.xml中的resources配置为以下内容:
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>win32-x86-64/opus.dll</include>
        </includes>
    </resource>
</resources>

2. 适配jar包运行场景

JNA无法直接加载jar包内部的dll文件,需要在程序初始化时先将dll解压到本地临时目录,再配置JNA的加载路径,示例代码如下:

// 在调用Native.load()之前执行这段代码
public static void loadOpusLibrary() throws IOException {
    // 读取jar包内的dll文件
    InputStream inputStream = YourClass.class.getResourceAsStream("/win32-x86-64/opus.dll");
    // 创建临时文件
    File tempDll = File.createTempFile("opus", ".dll");
    tempDll.deleteOnExit();
    
    // 写入临时文件
    FileOutputStream outputStream = new FileOutputStream(tempDll);
    byte[] buffer = new byte[1024];
    int read;
    while ((read = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, read);
    }
    inputStream.close();
    outputStream.close();
    
    // 配置JNA加载路径
    System.setProperty("jna.library.path", tempDll.getParent());
}

3. 验证打包与IDE运行配置

  • 打包后解压最终的mumble-client-gui的jar包,确认根目录下存在win32-x86-64/opus.dll文件
  • Eclipse环境下,确认sound模块的src/main/resources文件夹已被标记为Source Folder,运行时会自动加入类路径
  • 之前报32/64位不匹配的问题,可在cmd中执行dumpbin /headers opus.dll确认dll架构,和本地JDK架构保持一致即可,同时右键dll属性确认没有被系统锁定。

内容的提问来源于stack exchange,提问作者BeKoolMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 17:54:03