不知道具體原理,但是,加載 dll 文件時,帶路徑或者更改 dll 文件的名字,都會報錯。雖然庫記載成功了,但是處女座認為這不可接受。於是有了這個解決方案。
在根目錄為庫創建軟連接,然后使用 system.loadLibrary(“libname”) 來加載。事實證明,它時認軟連接的。
上代碼:
import com.seapine.surroundscm.api.*; import java.lang.UnsupportedOperationException; import java.lang.SecurityException; import java.io.IOException; import java.nio.file.*; public class SurroundSCMAPIExample { static { String arch = System.getProperty("sun.arch.data.model"); Path target; if(arch.equals("32")) { target = FileSystems.getDefault().getPath("lib","sscmapi.dll"); } else { target = FileSystems.getDefault().getPath("lib64","sscmapi.dll"); } Path link = FileSystems.getDefault().getPath("sscmapi.dll"); try { Files.deleteIfExists(link); Files.createSymbolicLink(link, target); } catch (IOException | UnsupportedOperationException | SecurityException e) { if (e instanceof SecurityException) { System.err.println("Permission denied!"); } if (e instanceof UnsupportedOperationException) { System.err.println("An unsupported operation was detected!"); } if (e instanceof IOException) { System.err.println("An I/O error occurred!"); } System.err.println(e); } System.loadLibrary("sscmapi"); } public static void main(String[] args) { SSCMContext context = new SSCMContext(); SSCMResult ret = SSCMAPI.connectRSA("localhost", 4900, "", "admin", "", context); if(SSCMAPI.SSCM_API_OK == ret.result) System.out.println("Connection Succeeded"); else System.out.println("Connection Failed: " + SSCMAPI.getLastError(ret.result)); SSCMAPI.disconnect(context); } }
妥妥的,再看不到任何的報錯。
不過這個方案有個坑,如果軟鏈接文件是 read-only 的,創建軟鏈接就有麻煩了,所以,在打包,或者是從倉庫 get 時,要主要刪掉軟鏈接文件。