How to create a shared library from Java code

Mitsunori Komatsu
Jul 17, 2018 · 2 min read

As you may know, GraalVM’s native-image command is capable of creating a shared library from Java code

An SVM image can be built as a standalone executable, which is the default, or as a shared library by passing --shared to native-image

It sounds very useful to call a Java library from C or something. Let’s try!

First, write a static method in Java:

Static methods that will be converted to a shared library need to have a specific annotation and accept Isolate/IsolateThread as the first argument.

Next, compile the class using javac

$ javac org/komamitsu/foobar/

Then, create a shared library using native-image command

$ native-image --shared -H:Name=libfoobar
Build on Server(pid: 66099, port: 64714)
classlist: 250.62 ms
(cap): 1,967.46 ms
setup: 2,497.16 ms
(typeflow): 3,190.13 ms
(objects): 1,842.08 ms
(features): 43.43 ms
analysis: 5,209.07 ms
universe: 216.93 ms
(parse): 1,053.24 ms
(inline): 561.35 ms
(compile): 3,761.77 ms
compile: 5,768.34 ms
image: 890.81 ms
write: 1,345.32 ms
[total]: 16,224.53 ms
$ ls -ltr
total 10328
drwxr-xr-x 3 komamitsu staff 102 Jul 16 21:43 org
-rw-r--r-- 1 komamitsu staff 334 Jul 17 22:05 libfoobar_dynamic.h
-rw-r--r-- 1 komamitsu staff 310 Jul 17 22:05 libfoobar.h
-rwxr-xr-x 1 komamitsu staff 5227120 Jul 17 22:05 libfoobar.dylib
-rw-r--r-- 1 komamitsu staff 3035 Jul 17 22:05 graal_isolate_dynamic.h
-rw-r--r-- 1 komamitsu staff 2939 Jul 17 22:05 graal_isolate.h

Of course, we need to write main function in C

This program does:

  • Create isolate
  • Call a function in the shared library that is generated from the Java static method. You can find the method name in libfoobar.h created by native-image --shared
  • Release the isolate

Let’s compile the C code with the static library and execute it.

$ cc -Wall -I. -L. -lfoobar main.c
$ ./a.out
42 + 13 = 55

It works!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade