3

I have a spring-boot web application that I would like to package as self executable jar. I have configured the plugin as described into the reference and java -jar myapp.jar shows that the app is trying to start.

However, my app needs a DLL, so I have added a static block inside my servlet initializer :

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

}

But I receive the UnsatisfiedLinkError exception.

How can I add a DLL to the embedded tomcat server ?

1 Answer 1

3

I suspect that your DLL library is included in the JAR file and can be loaded from classpath; if that's the case, all you need to do is copy that library somewhere on the file system where you can read it from.

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);
        }
    }
}
1
  • 1
    I think that should be load rather than loadLibrary. AFAIK, the latter requires the library to be available on the java.library.path whereas the former takes a file name. Sep 15, 2016 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.