« November 2009 | Main | August 2010 »

January 2010 Archives

January 28, 2010

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 (http://jackaudio.org) accessible to java.
There is already a javalib (http://jjack.berlios.de) with some features, there seems still some needes ones missing.

So i started today to have a look into SWIG (http://swig.org).

"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 ( http://www.swig.org/Doc1.3/SWIGDocumentation.html#Introduction) 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;
else 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.java
exampleJNI.java
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) {
exampleJNI.My_variable_set(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 (http://java.freehep.org/sandbox/G4Java).
There is also a maven plugin to use SWIG from within your maven build: http://java.freehep.org/freehep-swig-plugin.

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.

About January 2010

This page contains all entries posted to Philipps paper equivalent Blog in January 2010. They are listed from oldest to newest.

November 2009 is the previous archive.

August 2010 is the next archive.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.31