'Java Native Access'에 해당되는 글 1건

  1. Java Native Access 2011.05.13

Java Native AccessJava Native Access

Posted at 2011. 5. 13. 16:57 | Posted in 카테고리 없음

Java Native Access provides Java programs easy access to native shared libraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort. No boilerplate or generated glue code is required.

현재 JNA 최신 버전은 3.2.7 인데 최근에 3.2.8로 업데이트 되었다. 이것이 필요하게 된 이유는 JAVA를 이용하여 윈도우 프로그래밍을 할 경우 한계점이 발생하기 때문이다. 예를 들어 레지스트리를 제어(생성, 삽입, 삭제, 수정)하기 위해서는 JNA가 필요하다.

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
 
/** Simple example of native library declaration and usage. */
public class HelloWorld {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
            (Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
        void printf(String format, Object... args);
    }
 
    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, World\n");
        for (int i = 0; i < args.length; i++) {
            CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
        }
    }
}

위의 예제는 위키디피아에서 Java에서 printf 함수를 사용하여 "Hello, World"를 출력하는 소스 코드이다. 이것을 실행하기 위해서는 jna.jar 파일과 platform.jar 파일을 외부 라이브러리로 등록해 주어야 한다.

//