關於JAVA中的JNA

來源:互聯網
上載者:User

1、jna是什麼

jna是java native access的簡稱,用他可以調用C、C++代碼,特別是windows中強大的庫檔案(dll,在linux下是so檔案),這樣java就可以操控底層的一些東西,比如調用加密機、智慧卡之類的

2、jna下載

在maven下直接找jna就有,對應的jar包是jna-3.3.0.jar

3、調用msvcrt.dll(這是windows內建動態連結程式庫 ),不用找,他在system32下

Java代碼  
  1. package com.jnastudy.test;  
  2.   
  3. import com.sun.jna.Library;  
  4. import com.sun.jna.Native;  
  5. import com.sun.jna.Platform;  
  6. import com.sun.jna.platform.win32.Kernel32;  
  7. import com.sun.jna.platform.win32.User32;  
  8.   
  9. public class JNATest {  
  10.     //編寫一個介面,必須繼承Library,他要在載入庫檔案時用  
  11.     public interface CLibrary extends Library {  
  12.         //載入庫檔案,Platform.isWindows()可以判斷系統類別型  
  13.         CLibrary INSTANCE = (CLibrary) Native.loadLibrary(  
  14.                 (Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);  
  15.         //定義方法,與c語言標準的一致  
  16.         void printf(String format, Object... args);  
  17.     }  
  18.     public static void main(String[] args) {  
  19.         //調用  
  20.         CLibrary.INSTANCE.printf("%d----%f--",5,5.2f);  
  21.           
  22.         final User32 user32 = User32.INSTANCE;  
  23.         System.out.println(user32);  
  24.         final Kernel32 kernel32 = Kernel32.INSTANCE;  
  25.         System.out.println(kernel32.toString());  
  26.     }  
  27. }  

 4、java類型和C類型的映射



 

轉載:http://ligson.iteye.com/blog/1294518

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.