0
.

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 si...
Reasons for “going native”

•
•
•
•

Integration
Using operating system features (e.g: SWT)
Using architecture features (e...
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) {
Sy...
JNI Example (2/3)

// generated by javah from HelloJni.class
#include <jni.h>
/* ... /*
* Class:
HelloJni
* Method:
msgBox...
JNI Example (3/3)
#include <windows.h>
#include ”HelloJni.h”
JNIEXPORT jint JNICALL
Java_HelloJni_msgBox(
JNIEnv *env, job...
JNA Example
import com.sun.jna.*;
public class HelloJna {
public interface UserLib extends Library {
int MB_ICONINFORMATIO...
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
Upcoming SlideShare
Loading in...5
×

JNA

939

Published on

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
939
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "JNA"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×