【问题标题】:Spring-boot executable tomcat with DLL带有 DLL 的 Spring-boot 可执行 tomcat
【发布时间】:2016-09-15 14:17:42
【问题描述】:

我有一个 spring-boot Web 应用程序,我想将它打包为自可执行 jar。我已经按照referencejava -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 服务器?

【问题讨论】:

    标签: spring tomcat dll spring-boot


    【解决方案1】:

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

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

    【讨论】:

    • 我认为应该是load 而不是loadLibrary。 AFAIK,后者要求库在 java.library.path 上可用,而前者采用文件名。
    猜你喜欢
    • 2017-02-04
    • 2020-02-25
    • 1970-01-01
    • 2018-07-23
    • 2014-07-23
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多