creating JNI with Swig

I am currently playing around with JNI and Java due the colleagues question to make the connect features of jack-audio ( accessible to java.
There is already a javalib ( with some features, there seems still some needes ones missing.
So i started today to have a look into SWIG (
“SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.”
After some hours of research i ended up with some facts:
To created yourself a Java binding to a given c/c++ Program or Library you need one or more Interface files (*.I) and swig file with all the necessary swig module descriptions.
There is an example on the swig homepage ( to explain the workflow of SWIG.
There is a c file exmple.c:

/* File : example.c */
double  My_variable  = 3.0;

/* Compute factorial of n */
int  fact(int n) {
    if (n <= 1) 
        return 1;
        return n*fact(n-1);

/* Compute n mod m */
int my_mod(int n, int m) {
    return(n % m);

The mapping example.i files looks as the following:

/* File : example.i */
%module example
/* Put headers and other declarations here */
    extern double My_variable;
    extern int    fact(int);
    extern int    my_mod(int n, int m);
extern double My_variable;
extern int    fact(int);
extern int    my_mod(int n, int m);

As you can see, the Interface file has a similar syntax with some additional meta information.
You can now create your JNI bindings:

swig -java example.i
There are also flags for different other languages:
-allegrocl - Generate ALLEGROCL wrappers
-chicken - Generate CHICKEN wrappers
-clisp - Generate CLISP wrappers
-cffi - Generate CFFI wrappers
-csharp - Generate C# wrappers
-guile - Generate Guile wrappers
-java - Generate Java wrappers
-lua - Generate Lua wrappers
-modula3 - Generate Modula 3 wrappers
-mzscheme - Generate Mzscheme wrappers
-ocaml - Generate Ocaml wrappers
-octave - Generate Octave wrappers
-perl - Generate Perl wrappers
-php - Generate PHP wrappers
-pike - Generate Pike wrappers
-python - Generate Python wrappers
-r - Generate R (aka GNU S) wrappers
-ruby - Generate Ruby wrappers
-sexp - Generate Lisp S-Expressions wrappers
-tcl - Generate Tcl wrappers
-uffi - Generate Common Lisp / UFFI wrappers
-xml - Generate XML wrappers

As a result you get three new files:

  • example_wrap.c

The example_wrap.c can be used to compile the needed library file for your JNI access.
The two java Files are the basic JNI implementation:

    class exampleJNI {
        public final static native void My_variable_set(double jarg1);
        public final static native double My_variable_get();
        public final static native int fact(int jarg1);
        public final static native int my_mod(int jarg1, int jarg2);

And a basic java example how to access these functions:

public class example {
    public static void setMy_variable(double value) {
    public static double getMy_variable() {
        return exampleJNI.My_variable_get();
    public static int fact(int arg0) {
        return exampleJNI.fact(arg0);
    public static int my_mod(int n, int m) {
        return exampleJNI.my_mod(n, m);

To get into working with SWIG i can advise the sources of the G4Java Project.
There is also a maven plugin to use SWIG from within your maven build:
I am currently trying to create the necessary Interface files from the jack-audio sources to use them for a first run of SWIG. For python and tck you can use cmake to create these files.

You may also like...

2 Responses

  1. Dan Stieglitz says:

    I have been working on the same goal. I would like to be able to start a Jackd daemon from a Java process. Have you experimented with JNA ( It seems a better/simpler interface to native code than JNI. I have looked into it and am trying to learn how to construct the appropriate JNA wrapper code in Java.
    I have just downloaded SWIG but will need to learn that too… have you made any progress using SWIG?

  2. Philipp Haußleiter says:

    I am currently looking into This tool needs hopefully less hand-work. It is used in the JOGL API Project. I will post about it, when i got some more knowledge :-).

Leave a Reply