Your SlideShare is downloading. ×
JNA
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JNA

852
views

Published on

http://jsug.at/wiki/Meeting_59

http://jsug.at/wiki/Meeting_59

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
852
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. . JNA . Java Native Access Robert Bachmann JSUG/JUGAT Meeting #59 1
  • 2. 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
  • 3. 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
  • 4. Hello World with Windows API int WINAPI MessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType ); 4
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. Java type mappings • • • • • • int → int32_t short → int16_t long → int64_t String → char* byte → char char → int16_t 9
  • 10. JNA mapping classes • WString → wchar_t* • Pointer → void* • PlatformLong → long 10
  • 11. More features • • • • Array mapping Structures Callbacks Wrapper generator (JNAerator, third party) 11
  • 12. Implementation • Dispatch code implemented using JNI and libffi • jna.jar contains platform binaries 12
  • 13. Alternatives • BridJ • SWIG 13
  • 14. Links • • • https://github.com/twall/jna http://en.wikipedia.org/wiki/JNAerator http://www.swig.org/ 14
  • 15. Questions? 15
  • 16. Thanks Twitter @robertbachmann Email rb@ — .at 16

×