springboot动态加载native类库
生活随笔
收集整理的這篇文章主要介紹了
springboot动态加载native类库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有些時候需要使用到本地類庫來實現一些功能,比如在linux下使用jni去訪問so庫文件,這個時候就需要涉及庫文件的加載。本文介紹一下如何動態加載庫文件,即把庫文件放到工程項目里頭,方便工程的可移植性,然后在運行時去加載。
public class LibLoader {public static void loadLib(String libName) {String resourcePath = "/" + libName;String folderName = System.getProperty("java.io.tmpdir") + "/lib/";File folder = new File(folderName);folder.mkdirs();File libFile = new File(folder, libName);if (libFile.exists()) {System.load(libFile.getAbsolutePath());} else {try {InputStream in = LibLoader.class.getResourceAsStream(resourcePath);FileUtils.copyInputStreamToFile(in,libFile);in.close();System.load(libFile.getAbsolutePath());} catch (Exception e) {e.printStackTrace();throw new RuntimeException("Failed to load required lib", e);}}} }
將so文件放在工程的resources目錄下
使用
public class DemoJniClient{public native int helloWorld(String arg);static {LibLoader.loadLib("demo.so");} }總結
以上是生活随笔為你收集整理的springboot动态加载native类库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis及其Sentinel配置项详细
- 下一篇: spring cloud 微服务相关信息