我有一个 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/