如果您可以更改庫的加載代碼:將jar包含在jar/classpath中。然後將copy這個DLL文件複製到本地文件系統中。從那裏加載。以下假定庫位於類路徑中。
public void loadLibrary(String library) throws IOException {
InputStream source = getClass().getResourceAsStream(library);
File tempFile = File.createTempFile("javatmp", ".dll");
FileOutputStream dest = new FileOutputStream(tempFile);
try {
IOUtils.copy(source, dest)
}
finally {
dest.close();
source.close();
}
System.load(tempFile.getAbsolutePath());
tempFile.delete();
}
如果你不能改變如何庫加載DLL文件,那麼你可以修改(使用JNA)的路徑和DLL文件複製到該位置。以下假定庫位於類路徑的根目錄下。
public void exposeLibrary(String library, File tempDir) throws IOException {
InputStream source = getClass().getResourceAsStream(library);
File tempFile = new File(tempDir, library);
FileOutputStream dest = new FileOutputStream(tempFile);
try {
IOUtil.copy(source, dest)
}
finally {
dest.close();
source.close();
}
WinLibc.INSTANCE._putenv("PATH=" +
System.getenv().get("PATH") + File.pathSeparator + tempDir.getAbsolutePath());
// After this point System.loadLibrary(library) will load the DLL.
}
public interface WinLibC extends Library {
static WinLibC INSTANCE = Native.loadLibrary("msvcrt", WinLibC.class);
public int _putenv(String name);
}
注:我沒有測試任何這個。第二個解決方案將不適用於Linux(但可以與幾個changes。)
請參閱:http://stackoverflow.com/questions/7073039/how-to-include-native-library-on-mavens- java-library-path-variable?rq = 1 – Hossein
謝謝,這解決了我的一個問題(在測試服務器上運行測試)。但是每個開發人員仍然必須將PATH指向包含dll的目錄才能在本地運行代碼(例如從IntelliJ IDEA開始)。有沒有解決辦法? –