J ava
N ative
R untime

Java in the Box
Y. Sakuraba
Agenda

Background
JNR
How To Use JNR

Project Panama
Background
Standard Interface
JaVa to Call Native Code

 javah

Interface Need to Modify C/ C++
Hard to Use
JNl rapper

 

Java
Native
Access Easy to Use

No javah

Performance . ... ... . . ..
_ Two is Too lfiignyg _
One is Not Enough
Java
Ngtjye
Runtime
Author: 

Charles Nutter
JRuby Guy
Problems in JRuby

OS Level:  Filesystem,  spawn. ..
Library:  Graphics,  Crypto,  NoSQL
C ext Replacement/ Transition
Lan...
Easy to Use

Java
N ativ e Good Performance

Runtime Many Features
GitHub https: //github. com/ jnr
Java jnr-ffi Main Part

» jffi FFl for Java
Nalylve jnr-posix POSIX
Runtlme jnr-constants D...
J

   

NI JNR
mm: 

JNI Call

   
 

Java World

Native World
JNI lmpl JNI lmpl

W

Target Lib.
Foreign Function Interface

Interface to Call C/ C++
for High-Level Language

Iibffi
Java Foreign Function Interface

Iibffi based

Low-Level Lib
Broad Platform Support

Darwin universal
Linux x86, X64, ARM, ...
How to Use JNR
Getting PID

public class GetPidJNI {
 public static native long getpid(); 
‘I’.  t’ - -
JaVa :  gys%erli. load("pid");  l...
<3’: -at: ;til-r. ig i i ‘It i

 . =.l‘V. /,= .Il“l Ciiisliiicri .  6;‘-or-—*“C: F"'i ciETlIE‘. _ii

oii; i.'ifi. i9.~ I-_...
Getting PID

 |  #include "demo_GetPidJNI. h"

jlong JNICALL

Java_demo_GetPidJNI_ etpid(JNIEnv *env
Java g jclass c) {, 
...
Getting PHD

 

public class GetPidJNR {
JdF§ public interface GetF'i {

} long getpid(); 

I public static void main(Stri...
POSIX

POSIX posix =  POSIXFactory. getPOSIX(); 

int pid

_ .  posix. getpid(); 
int uid

posix. getuid(); 

Enable ta Us...
UNIX Domain Socket

File path =  new java. io. File("/ tmp/ tmp-socket"); 
UnixSocketAddress address
=  new UnixSocketAddr...
Signal

Signal s new Signal("HUP"); 
Signal. handle(s, 
signal -> {
/ / Do something
});
Project
Panama
Project Pglama

lnterconnecting
JVM and Native Code

Lead:  John Rose
Project Pglama

Array 2.0
Native Method Handle

Native Memory Layout
JEP 191: FFI
User Code

If
J NR Stub
If
JNI Ca" I‘ I? :ri: fI V 9 I I liiéifl >-pe'j"J J H

JNl

{L
lmpl

mt? ‘ *’* ’_I. gf  -
jffi I V ,...
Conclusion

JNI:  Hard to Use
JNA:  Low Performance

» JNR Easy to Use

Good Performance
Many Features

I} Panama FFI
J ava
N ative
R untime

Java in the Box
Y. Sakuraba
JNR: Java Native Runtime
JNR: Java Native Runtime
Upcoming SlideShare
Loading in...5
×

JNR: Java Native Runtime

299

Published on

JJUG Night Seminar 2015 Aug.
"JNR: Java Native Runtime"

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
299
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Transcript of "JNR: Java Native Runtime"

  1. 1. J ava N ative R untime Java in the Box Y. Sakuraba
  2. 2. Agenda Background JNR How To Use JNR Project Panama
  3. 3. Background
  4. 4. Standard Interface JaVa to Call Native Code javah Interface Need to Modify C/ C++ Hard to Use
  5. 5. JNl rapper Java Native Access Easy to Use No javah Performance . ... ... . . ..
  6. 6. _ Two is Too lfiignyg _ One is Not Enough
  7. 7. Java Ngtjye Runtime
  8. 8. Author: Charles Nutter JRuby Guy
  9. 9. Problems in JRuby OS Level: Filesystem, spawn. .. Library: Graphics, Crypto, NoSQL C ext Replacement/ Transition Lang & Runtime: LLVM, Chang, V8
  10. 10. Easy to Use Java N ativ e Good Performance Runtime Many Features
  11. 11. GitHub https: //github. com/ jnr Java jnr-ffi Main Part » jffi FFl for Java Nalylve jnr-posix POSIX Runtlme jnr-constants Define Constants jnr-unixsoeket UNIX Socket jnr-signal Handle Signal et al.
  12. 12. J NI JNR mm: JNI Call Java World Native World JNI lmpl JNI lmpl W Target Lib.
  13. 13. Foreign Function Interface Interface to Call C/ C++ for High-Level Language Iibffi
  14. 14. Java Foreign Function Interface Iibffi based Low-Level Lib Broad Platform Support Darwin universal Linux x86, X64, ARM, PPC. .. Windows x86, x64 l FreeBSD/ OpenBSD x86,x64 g Solaris x86,x64,Sparc et al.
  15. 15. How to Use JNR
  16. 16. Getting PID public class GetPidJNI { public static native long getpid(); ‘I’. t’ - - JaVa : gys%erli. load("pid"); load | 'bp'd°s° javah public static void main(String. .. args ) { getpid();
  17. 17. <3’: -at: ;til-r. ig i i ‘It i . =.l‘V. /,= .Il“l Ciiisliiicri . 6;‘-or-—*“C: F"'i ciETlIE‘. _ii oii; i.'ifi. i9.~ I-_"i; i 3|}? !-1 l, lllll Iii L r‘. ’i"'i riiC ‘l. 't. k,CZi= <1f'l rr"i . lfl. " it/ 'c ii if i **”** artetti‘“T“ i iii .4 . TIBET/ iiEZ‘CF'Cl: '”‘ iii ‘i. «m; —i TliLI1<f. A. L- r‘ F ‘F TL. ’-. l‘V. /,= .l , Cil§lI'IlC>_, ¢l§~"lIE’”'lCETIMI; if ? Ii[I. jz~'. rry , , f‘i C ‘L. =.: :.; : J g; ‘l _, 'i‘
  18. 18. Getting PID | #include "demo_GetPidJNI. h" jlong JNICALL Java_demo_GetPidJNI_ etpid(JNIEnv *env Java g jclass c) {, javah } return getpid(); C/ C++
  19. 19. Getting PHD public class GetPidJNR { JdF§ public interface GetF'i { } long getpid(); I public static void main(String. .. args ) { GetPid getpid ‘ = LibraryLoader. create(CetPid. class) . load("c"); F W: x: getpid. getpid();
  20. 20. POSIX POSIX posix = POSIXFactory. getPOSIX(); int pid _ . posix. getpid(); int uid posix. getuid(); Enable ta Use Windows
  21. 21. UNIX Domain Socket File path = new java. io. File("/ tmp/ tmp-socket"); UnixSocketAddress address = new UnixSocketAddress(path); UnixSocketChannel channel = UnixSocketChannel. open(address); InputStream stream = Channels. newInputStream(channel);
  22. 22. Signal Signal s new Signal("HUP"); Signal. handle(s, signal -> { / / Do something });
  23. 23. Project Panama
  24. 24. Project Pglama lnterconnecting JVM and Native Code Lead: John Rose
  25. 25. Project Pglama Array 2.0 Native Method Handle Native Memory Layout JEP 191: FFI
  26. 26. User Code If J NR Stub If JNI Ca" I‘ I? :ri: fI V 9 I I liiéifl >-pe'j"J J H JNl {L lmpl mt? ‘ *’* ’_I. gf - jffi I V ,9. . LI . ~_ ’ ‘V. i q ‘W I my ‘: ’/| .?iaI , .:vI‘I@; .:(; Target Libs
  27. 27. Conclusion JNI: Hard to Use JNA: Low Performance » JNR Easy to Use Good Performance Many Features I} Panama FFI
  28. 28. J ava N ative R untime Java in the Box Y. Sakuraba
  1. A particular slide catching your eye?

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

×