System.loadLibrary()的使用方法匯總

來源:互聯網
上載者:User

http://blog.csdn.net/forandever/archive/2010/11/03/5983846.aspx

 

當使用System.loadLibrary()調用 Dll,兩種方法:

1.設定環境變數。

比如:所編輯的Dll在目錄“D:/cppProjects/nativecode/release”內,將這個路徑複製添加到電腦的環境變數中的path變數內即可。

2.設定項目屬性。(開發推薦)

右擊項目名|選擇屬性properties|在左邊列表內選擇“Java Build Path”|在右邊選項卡用選擇“source”|點開項目名前的“+”號,選擇“Native library location”,“Edit”選擇上面“D:/cppProjects/nativecode/release”路徑。(當然如果將dll拷貝到workspace下也可以用相對路徑。也可右擊“src”設定其properties內Native Library項。)

System.load 和 System.loadLibrary詳解

1.它們都可以用來裝載庫檔案,不論是JNI庫檔案還是非JNI庫檔案。在任何本地方法被調用之前必須先用這個兩個方法之一把相應的JNI庫檔案裝載。

2.System.load 參數為庫檔案的絕對路徑,可以是任意路徑。
例如你可以這樣載入一個windows平台下JNI庫檔案:
System.load("C://Documents and Settings//TestJNI.dll");。

3. System.loadLibrary 參數為庫檔案名稱,不包含庫檔案的副檔名。
例如你可以這樣載入一個windows平台下JNI庫檔案
System. loadLibrary ("TestJNI");

這裡,TestJNI.dll 必須是在java.library.path這一jvm變數所指向的路徑中。
可以通過如下方法來獲得該變數的值:
System.getProperty("java.library.path");
預設情況下,在Windows平台下,該值包含如下位置:
1)和jre相關的一些目錄
2)程式目前的目錄
3)Windows目錄
4)系統目錄(system32)
5)系統內容變數path指定目錄

4.如果你要載入的庫檔案靜態連結到其它動態連結程式庫,例如TestJNI.dll 靜態連結到dependency.dll, 那麼你必須注意:
1)如果你選擇
System.load("C://Documents and Settings// TestJNI.dll");
那麼即使你把dependency.dll同樣放在C://Documents and Settings//下,load還是會因為找不到依賴的dll而失敗。因為jvm在載入TestJNI.dll會先去載入TestJNI.dll所依賴的庫檔案dependency.dll,而dependency.dll並不位於java.library.path所指定的目錄下,所以jvm找不到dependency.dll。
你有兩個方法解決這個問題:一是把C://Documents and Settings//加入到java.library.path的路徑中,例如加入到系統的path中。二是先調用
System.load("C://Documents and Settings// dependency.dll"); 讓jvm先載入dependency.dll,然後再調用System.load("C://Documents and Settings// TestJNI.dll");
2)如果你選擇
System. loadLibrary ("TestJNI");
那麼你只要把dependency.dll放在任何java.library.path包含的路徑中即可,當然也包括和TestJNI.dll相同的目錄。

轉載:

http://blog.csdn.net/ring0hx/archive/2008/11/06/3242245.aspx

http://blog.csdn.net/grtwall/archive/2009/03/03/3954328.aspx

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/forandever/archive/2010/11/03/5983846.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.