JNA
Upcoming SlideShare
Loading in...5
×
 

JNA

on

  • 687 views

http://jsug.at/wiki/Meeting_59

http://jsug.at/wiki/Meeting_59

Statistics

Views

Total Views
687
Views on SlideShare
685
Embed Views
2

Actions

Likes
0
Downloads
5
Comments
0

1 Embed 2

https://twitter.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

JNA JNA Presentation Transcript

  • . JNA . Java Native Access Robert Bachmann JSUG/JUGAT Meeting #59 1
  • Motivation • JNI (Java Native Interface) allows Java programs to call native code • JNA is an open-source library that simpli es using JNI (Java Native Interface) 2
  • Reasons for “going native” • • • • Integration Using operating system features (e.g: SWT) Using architecture features (e.g: SSE, RdRand) Performance (e.g: tomcat-native) 3
  • Hello World with Windows API int WINAPI MessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType ); 4
  • JNI Example (1/3) public class HelloJni { public native int msgBox(String s); public static void main(String[] args) { System.loadLibrary(”HelloJni”); new HelloJni().msgBox(”Hello␣World!”); } } 5
  • JNI Example (2/3) // generated by javah from HelloJni.class #include <jni.h> /* ... /* * Class: HelloJni * Method: msgBox * Signature: (Ljava/lang/String;)I */ JNIEXPORT jint JNICALL Java_HelloJni_msgBox (JNIEnv *, jobject, jstring); 6
  • JNI Example (3/3) #include <windows.h> #include ”HelloJni.h” JNIEXPORT jint JNICALL Java_HelloJni_msgBox( JNIEnv *env, jobject o, jstring s) { const char *ns = (*env)->GetStringUTFChars(env, s, NULL); int i = MessageBoxA( NULL, ns, ”Demo”, MB_ICONINFORMATION); (*env)->ReleaseStringUTFChars(env, s, ns); return i; } 7
  • JNA Example import com.sun.jna.*; public class HelloJna { public interface UserLib extends Library { int MB_ICONINFORMATION = 0x40; int MessageBoxA(Pointer p, String s, String t, int type); } public static void main(String[] args) { UserLib lib = (UserLib) Native.loadLibrary(”user32”, UserLib.class); lib.MessageBoxA(null, ”Hello␣World!”, ”Demo”, lib.MB_ICONINFORMATION); } } 8
  • Java type mappings • • • • • • int → int32_t short → int16_t long → int64_t String → char* byte → char char → int16_t 9
  • JNA mapping classes • WString → wchar_t* • Pointer → void* • PlatformLong → long 10
  • More features • • • • Array mapping Structures Callbacks Wrapper generator (JNAerator, third party) 11
  • Implementation • Dispatch code implemented using JNI and libffi • jna.jar contains platform binaries 12
  • Alternatives • BridJ • SWIG 13
  • Links • • • https://github.com/twall/jna http://en.wikipedia.org/wiki/JNAerator http://www.swig.org/ 14
  • Questions? 15
  • Thanks Twitter @robertbachmann Email rb@ — .at 16