6
7 1. Locally build "foreign" branch of panama repo [http://hg.openjdk.java.net/panama/dev/](http://hg.openjdk.java.net/panama/dev/)
8 2. Download pre-built panama "foreign" early access binaries from [http://jdk.java.net/panama/](http://jdk.java.net/panama/)
9
10 Using foreign function call in Java involves the following three steps:
11
12 1. Use **jextract** tool to generate java interface for your C header file(s)
13 2. Use **java.foreign** API to create ("bind") implementation for C header interfaces
14 3. Invoke C functions via the jextracted Java interface
15
16 ## Embedding Python interpreter in your Java program (Mac OS)
17
18 ### jextract a Jar file for Python.h
19
20 ```sh
21
22 jextract -l python2.7 \
23 -rpath /System/Library/Frameworks/Python.framework/Versions/2.7/lib \
24 --exclude-symbols .*_FromFormatV\|_.*\|PyOS_vsnprintf\|.*_VaParse.*\|.*_VaBuild.*\|PyBuffer_SizeFromFormat\|vasprintf\|vfprintf\|vprintf\|vsprintf \
25 -t org.python \
26 /usr/include/stdio.h /usr/include/stdlib.h /usr/include/python2.7/Python.h \
27 -o python.jar
28
29 ```
30
31 ### Java program that uses extracted Python interface
32
33 ```java
34
35 // import java.foreign packages
36 import java.foreign.Libraries;
37 import java.foreign.Scope;
38 import java.foreign.memory.Pointer;
39
40 // import jextracted python 'header' classes
41 import static org.python.Python_h.*;
42 import static org.python.pythonrun_h.*;
43
44 public class PythonMain {
45 public static void main(String[] args) {
46 Py_Initialize();
58 ### Running the Java code that calls Python interpreter
59
60 ```sh
61
62 javac -cp pythor.jar PythonMain.java
63
64 java -cp python.jar:. PythonMain
65
66 ```
67
68 ## Embedding Python interpreter in your Java program (Ubuntu 16.04)
69
70 ### jextract a Jar file for Python.h
71
72 ```sh
73
74 jextract -l python2.7 \
75 -rpath /usr/lib/python2.7/config-x86_64-linux-gnu \
76 --exclude-symbols .*_FromFormatV\|_.*\|PyOS_vsnprintf\|.*_VaParse.*\|.*_VaBuild.*\|PyBuffer_SizeFromFormat\|vasprintf\|vfprintf\|vprintf\|vsprintf \
77 -t org.python \
78 /usr/include/stdio.h /usr/include/stdlib.h /usr/include/python2.7/Python.h \
79 -o python.jar
80
81 ```
82
83 ### Compiling and Running Python Java example
84
85 Follow the instructions from the Mac OS section
86
87 ## Using BLAS library
88
89 BLAS is a popular library that allows fast matrix and vector computation: [http://www.netlib.org/blas/](http://www.netlib.org/blas/).
90
91 ### Installing OpenBLAS (Mac OS)
92
93 On Mac, blas is available as part of the OpenBLAS library: [https://github.com/xianyi/OpenBLAS/wiki](https://github.com/xianyi/OpenBLAS/wiki)
94
95 OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.
96
97 You can install openblas using HomeBrew
98
445 System.out.printf("%d %s\n", pid, procName);
446 }
447 }
448 }
449 }
450
451 ```
452
453 ### Running the Java code that uses libproc
454
455 ```sh
456
457 javac -cp libproc.jar LibprocMain.java
458
459 java -cp libproc.jar:. LibprocMain
460
461 ```
462
463 ## Using readline library from Java code (Mac OS)
464
465 ### jextract a jar file for readline.h
466
467 ```sh
468
469 jextract -l readline -rpath /usr/local/opt/readline/lib/ \
470 -t org.unix \
471 /usr/include/readline/readline.h /usr/include/_stdio.h \
472 --exclude-symbol readline_echoing_p -o readline.jar
473
474 ```
475
476 ### Java code that uses readline
477
478 ```java
479
480 import java.foreign.*;
481 import java.foreign.memory.*;
482 import static org.unix.readline_h.*;
483
484 public class Readline {
485 public static void main(String[] args) {
486 // Scope for native allocations
487 try (Scope s = Scope.newNativeScope()) {
488 // allocate C memory initialized with Java string content
489 var pstr = s.allocateCString("name? ");
490
491 // call "readline" API
|
6
7 1. Locally build "foreign" branch of panama repo [http://hg.openjdk.java.net/panama/dev/](http://hg.openjdk.java.net/panama/dev/)
8 2. Download pre-built panama "foreign" early access binaries from [http://jdk.java.net/panama/](http://jdk.java.net/panama/)
9
10 Using foreign function call in Java involves the following three steps:
11
12 1. Use **jextract** tool to generate java interface for your C header file(s)
13 2. Use **java.foreign** API to create ("bind") implementation for C header interfaces
14 3. Invoke C functions via the jextracted Java interface
15
16 ## Embedding Python interpreter in your Java program (Mac OS)
17
18 ### jextract a Jar file for Python.h
19
20 ```sh
21
22 jextract -l python2.7 \
23 -rpath /System/Library/Frameworks/Python.framework/Versions/2.7/lib \
24 --exclude-symbols .*_FromFormatV\|_.*\|PyOS_vsnprintf\|.*_VaParse.*\|.*_VaBuild.*\|PyBuffer_SizeFromFormat\|vasprintf\|vfprintf\|vprintf\|vsprintf \
25 -t org.python \
26 /usr/include/python2.7/Python.h \
27 -o python.jar
28
29 ```
30
31 ### Java program that uses extracted Python interface
32
33 ```java
34
35 // import java.foreign packages
36 import java.foreign.Libraries;
37 import java.foreign.Scope;
38 import java.foreign.memory.Pointer;
39
40 // import jextracted python 'header' classes
41 import static org.python.Python_h.*;
42 import static org.python.pythonrun_h.*;
43
44 public class PythonMain {
45 public static void main(String[] args) {
46 Py_Initialize();
58 ### Running the Java code that calls Python interpreter
59
60 ```sh
61
62 javac -cp pythor.jar PythonMain.java
63
64 java -cp python.jar:. PythonMain
65
66 ```
67
68 ## Embedding Python interpreter in your Java program (Ubuntu 16.04)
69
70 ### jextract a Jar file for Python.h
71
72 ```sh
73
74 jextract -l python2.7 \
75 -rpath /usr/lib/python2.7/config-x86_64-linux-gnu \
76 --exclude-symbols .*_FromFormatV\|_.*\|PyOS_vsnprintf\|.*_VaParse.*\|.*_VaBuild.*\|PyBuffer_SizeFromFormat\|vasprintf\|vfprintf\|vprintf\|vsprintf \
77 -t org.python \
78 /usr/include/python2.7/Python.h \
79 -o python.jar
80
81 ```
82
83 ### Compiling and Running Python Java example
84
85 Follow the instructions from the Mac OS section
86
87 ## Using BLAS library
88
89 BLAS is a popular library that allows fast matrix and vector computation: [http://www.netlib.org/blas/](http://www.netlib.org/blas/).
90
91 ### Installing OpenBLAS (Mac OS)
92
93 On Mac, blas is available as part of the OpenBLAS library: [https://github.com/xianyi/OpenBLAS/wiki](https://github.com/xianyi/OpenBLAS/wiki)
94
95 OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.
96
97 You can install openblas using HomeBrew
98
445 System.out.printf("%d %s\n", pid, procName);
446 }
447 }
448 }
449 }
450
451 ```
452
453 ### Running the Java code that uses libproc
454
455 ```sh
456
457 javac -cp libproc.jar LibprocMain.java
458
459 java -cp libproc.jar:. LibprocMain
460
461 ```
462
463 ## Using readline library from Java code (Mac OS)
464
465 ### Note: This sample fails because of too big UTF-8 String in NativeHeader annotation
466
467 ### jextract a jar file for readline.h
468
469 ```sh
470
471 jextract -l readline -rpath /usr/local/opt/readline/lib/ \
472 -t org.unix \
473 /usr/include/readline/readline.h \
474 --exclude-symbol readline_echoing_p -o readline.jar
475
476 ```
477
478 ### Java code that uses readline
479
480 ```java
481
482 import java.foreign.*;
483 import java.foreign.memory.*;
484 import static org.unix.readline_h.*;
485
486 public class Readline {
487 public static void main(String[] args) {
488 // Scope for native allocations
489 try (Scope s = Scope.newNativeScope()) {
490 // allocate C memory initialized with Java string content
491 var pstr = s.allocateCString("name? ");
492
493 // call "readline" API
|