« Wiederherstellen eines MacOS Festplatten-Backups mit Hilfe von DD | Main | Philipps 5 mins: Graph-Fun with AJAX and Canvas »

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.

TrackBack

TrackBack URL for this entry:
http://www.innoq.com/movabletype/mt-tb.cgi/3105

Comments (3)

Dan Stieglitz:

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 (http://jna.dev.java.net)? 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?

Philipp Hau├čleiter:

I am currently looking into https://gluegen.dev.java.net. 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 :-).

Approvingly your article helped me truly much in my college assignment. Hats high to you dispatch, wish look audacious in behalf of more interdependent articles promptly as its united of my choice issue to read.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on January 28, 2010 12:45 AM.

The previous post in this blog was Wiederherstellen eines MacOS Festplatten-Backups mit Hilfe von DD.

The next post in this blog is Philipps 5 mins: Graph-Fun with AJAX and Canvas.

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

Powered by
Movable Type 3.31