Using Panama "foreign" JDK

Using Panama "foreign" JDK

There are two ways to get a panama foreign branch JDK.

Locally build "foreign" branch of panama repo http://hg.openjdk.java.net/panama/dev/ Download pre-built panama "foreign" early access binaries from http://jdk.java.net/panama/

Using foreign function call in Java involves the following three steps:

Use jextract tool to generate java interface for your C header file(s) Use java.foreign API to create ("bind") implementation for C header interfaces Invoke C functions via the jextracted Java interface

Windows notes

You will (almost always) need to have Visual Studio installed, since most libraries indirectly depend on Visual Studio runtime libraries and this currently means that jextract needs their header to extract successfully. Windows examples have been tested with Build Tools for Visual Studio 2017.

It is generally a good idea to give jextract a bunch of extra memory since a lot of big system headers are transitively included. The extra memory will make the jextract run significantly faster. Windows support was added only recently, and the memory usage of jextract has not been optimized yet, so this is a workaround. You can give extra memory by passing e.g. -J-Xmx8G to jextract as an additional argument, which in this example gives jextract 8 gigabytes of memory.

Commands are tested in PowerShell.

Embedding Python interpreter in your Java program (Mac OS)

jextract a Jar file for Python.h

Java program that uses extracted Python interface

Running the Java code that calls Python interpreter

jlinking Python Interpreter in your JDK (Mac OS)

Generating jmod using jextract

Jlinking python module to create a JDK with Python in it

jdk.compiler and org.python modules are added to the generated (jlinked) JDK

Compile and run user code with "pythonjdk" jdk

In the following commands, it is assumed that you've put $pythonjdk/bin in your $PATH

Embedding Python interpreter in your Java program (Ubuntu 16.04)

jextract a Jar file for Python.h

Compiling and Running Python Java example

Follow the instructions from the Mac OS section

Embedding Python interpreter in your Java program (Windows)

jextract a Jar file for Python.h

Where python 2.7 is installed in the C:\Python27 directory:

Compiling and Running Python Java example

Using sqlite3 library in your Java program (Mac OS)

jextract a jar file for sqlite3.h

Java sample that uses sqlite3 library

Compiling and Running sqlite Java example

Using sqlite3 library in your Java program (Ubuntu 16.04)

Installing sqlite3

On Ubuntu (16.04) to install sqlite3 headers and libraries the following command is required:

This should install the sqlite3 header (under /usr/include ), as well as the sqlite3 shared library (under /usr/lib/x86_64-linux-gnu ).

jextract a jar file for sqlite3.h

To extract sqlite, run the following command:

Compiling and Running sqlite Java example

Please refer to the Mac OS instructions; once the library as been extracted (as per the instructions above), the sample program shown in that section should work on Ubuntu as well.

Using BLAS library

BLAS is a popular library that allows fast matrix and vector computation: http://www.netlib.org/blas/.

Installing OpenBLAS (Mac OS)

On Mac, blas is available as part of the OpenBLAS library: https://github.com/xianyi/OpenBLAS/wiki

OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.

You can install openblas using HomeBrew

It installs include and lib directories under /usr/local/opt/openblas

Installing OpenBLAS (Ubuntu 16.04)

On Ubuntu, blas is distributed as part of the atlas library: http://math-atlas.sourceforge.net/.

You can install atlas using apt

This command will install include files under /usr/include/atlas and corresponding libraries under /usr/lib/atlas-dev .

jextracting cblas.h (MacOS)

The following command can be used to extract cblas.h on MacOs

The FORCE_OPENBLAS_COMPLEX_STRUCT define is needed because jextract does not yet handle C99 _Complex types. The rest of the options are standard ones.

jextracting cblas.h (Ubuntu 16.04)

The following command can be used to extract cblas.h on Ubuntu

Java sample code that uses cblas library

Compiling and running the above cblas samples

Using LAPACK library (Ubuntu)

On Ubuntu, the same steps used to install the blas (via atlas) library also install headers and libraries for the LAPACK library, a linear algebra computation library built on top of blas.

jextracting clapack.h (Ubuntu 16.04)

The following command can be used to extract the LAPACK header:

Java sample code that uses LAPACK library

Compiling and running the above LAPACK sample

Using LAPACK library (Mac OS)

On Mac OS, lapack is installed under /usr/local/opt/lapack directory.

jextracting lapacke.h

Java sample code that uses LAPACK library

Compiling and running the above LAPACK sample

Using libproc library to list processes from Java (Mac OS)

jextract a jar file for libproc.h

jextract -t org.unix -lproc -L /usr/lib --record-library-path -o libproc.jar /usr/include/libproc.h

Java program that uses libproc to list processes

Running the Java code that uses libproc

Using readline library from Java code (Mac OS)

jextract a jar file for readline.h

Java code that uses readline

Running the java code that uses readline

javac -cp readline.jar Readline.java java -cp readline.jar:. Readline

Using libcurl from Java (Mac OS)

jextract a jar for curl.h

Java code that uses libcurl

Running the java code that uses libcurl

Using unistd.h from Java code (Linux)

jextract a jar file for unistd.h

Java code that calls getpid

Running the Java code that uses getpid

Using OpenGL graphic library (Ubuntu 16.04)

OpenGL is a popular portable graphic library: https://www.opengl.org/

Installing OpenGL (Ubuntu 16.04)

Installing relevant OpenGL headers and libraries can be a bit tricky, as it depends on what graphic card is installed on the target platform. The following instruction assume that the standard version of OpenGL is used (e.g. mesa), rather than a proprietary one (Nvidia or AMD), although the changes to get these working are rather small.

OpenGL is always coupled with a bunch of other libraries, namely GLU and glut. You can install all those libraries using apt , as follows:

If the installation was successful, OpenGL headers can be found under /usr/include/GL , while libraries can be found in the folder /usr/lib/x86_64-linux-gnu/ .

jextracting OpenGL (Ubuntu 16.04)

To extract the opengl libraries the following command suffices:

Since glut depends on the other libraries (GLU and GL), it is not necessary to give additional headers to jextract.

Java sample code that uses the OpenGL library

Running the Java code that uses OpenGL (Ubuntu 16.04)

Using OpenGL graphic library (Windows)

Installing OpenGL

Download the freeglut package for MSVC at https://www.transmissionzero.co.uk/software/freeglut-devel/

Extract the freeglut zip.

jextracting OpenGL

Navigate to the root directory of the extracted zip and run the following commands:

The directory that is assigned to $inc is an example, and is system dependent. Make sure that the build number at the end of the path (in this case 10.0.17134.0 ) is the latest one found in the parent folder ( C:\Program Files (x86)\Windows Kits\10\Include\ ).

There are a bunch of warnings generated, but as long as the jar file is generated in the working directory the extraction was successful.

Java sample code that uses the OpenGL library

This is the same as in the Ubuntu section

Running the Java code that uses OpenGL

Using TensorFlow C API (Mac OS)

Quoted from https://www.tensorflow.org/install/lang_c

"TensorFlow provides a C API that can be used to build bindings for other languages. The API is defined in c_api.h and designed for simplicity and uniformity rather than convenience."

Installing libtensorflow

You can follow the setup procedure as described in the above page.

Alternatively, on Mac, you can install libtensorflow using HomeBrew

Tensorflow ship the libtensorflow with an .so extension, this doesn't work well for java on MacOS as java expect .dylib extension. To work around this, create a symbolic link.

jextracting libtensorflow c_api.h

The following command can be used to extract c_api.h.

The caveat to extract tensorflow C API is that it declare function prototype without argument in C++ style, for example, TF_Version(), which is considered incomplete C function prototype instead of C style as in TF_Version(void). An incomplete function prototype will become vararg funciton. To avoid that, we need to pass clang '-x c++' options to jextract with '-C -x -C c++'

Java sample code that uses tensorflow library

Compiling and running the above TensorFlow sample

Using TensorFlow C API (Windows)

Installing libtensorflow

You can download a binary distribution from https://www.tensorflow.org/install/lang_c

Extract the zip file.

jextracting libtensorflow c_api.h

The problem outlined in the Mac OS instruction w.r.t. incorrect function prototypes still exists (though it has been solved in the Tensorflow github repository, this change has not yet made it into the binary distributions). On Windows there is however no jextract command that works around this, so the only way to extract the \include\c\c_api.h header successfully is to first manually fix the incorrect function prototypes:

Once you've done this you can use the following jextract command from the libtensorflow root directory:

Java sample code that uses tensorflow library

This is the same as for the Mac OS section.

Compiling and running the above TensorFlow sample