带 DLL 的 Spring-boot 可执行 tomcat

标签 spring tomcat dll spring-boot

我有一个 spring-boot web 应用程序,我想将其打包为可自执行的 jar。我已经按照 reference 中的描述配置了插件java -jar myapp.jar 显示该应用正在尝试启动。

但是,我的应用程序需要一个 DLL,所以我在我的 servlet 初始化程序中添加了一个静态 block :

public class DllUsageWebApp extends SpringBootServletInitializer {
    static {
        System.loadLibrary("TheLibrary.dll");
    }
    public static void main(String[] args) {
        SpringApplication.run(DllUsageWebApp .class, args);
    }

}

但是我收到了 UnsatisfiedLinkError 异常。

如何将 DLL 添加到嵌入式 tomcat 服务器?

最佳答案

我怀疑你的DLL库包含在JAR文件中,可以从classpath加载;如果是这种情况,您需要做的就是将该库复制到文件系统上您可以从中读取的某个位置。

public class DllUsageWebApp extends SpringBootServletInitializer {
    static {
        String tempLibraryFile = 
          copyResourceToTempDirFile("/path/to/dll/in/JAR", "my.dll");
        System.load(tempLibraryFile.absolutePath());
    }

    public static void main(String[] args) {
        SpringApplication.run(DllUsageWebApp .class, args);
    }

    public static File copyResourceToTempDirFile(
        String resourcePath, String destinationFileName) {
        File tempDir = new File(System.getProperty("java.io.tmpdir"));
        File tempDirFile = new new File(tempDir, destinationFileName);
        try (InputStream input = resourceAsStream(resourcePath);
            OutputStream output = new FileOutputStream(tempDirFile)) {
            IOUtils.copy(input, output);
            tempDirFile.deleteOnExit();
            return tempDirFile;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

关于带 DLL 的 Spring-boot 可执行 tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39513467/

相关文章:

c++ - 邮寄消息 : How to post a message to a process that does not have a window?

javascript - 如何在 Spring 中使用 JSTL 在下拉列表中显示数据?

java - 使用 Spring,@InjectMock 注释测试目标不使用模拟

java - Spring属性解密

java - 在基于表单的身份验证(tomcat 领域)的情况下,如何强制 tomcat 始终将第一个请求重定向到登录页面

java - 连接池中的 Tomcat-Mysql 连接在空闲时间后超时

java - Spring Data JPA 没有类型合格的 bean ... 找到依赖项

maven - 配置文件相关的 web.xml

c# - 每个目标 .NET 平台的编译是否不同?

delphi - 我应该如何在 Delphi 6 中调用这个特定的 dll 函数