簡體   English   中英

帶有DLL的Spring-boot可執行tomcat

[英]Spring-boot executable tomcat with DLL

我有一個Spring-boot Web應用程序,我想將其打包為自可執行jar。 我已經按照參考資料中所述配置了插件,並且java -jar myapp.jar顯示該應用正在嘗試啟動。

但是,我的應用程序需要一個DLL,因此我在servlet初始化程序中添加了一個靜態塊:

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM