Java and C together
It may seem antithetical, but your bloated java code can utilise your nice lean C libraries quite easily using the JNI interface.
There are two major tasks to get your C stuff into Java:
1) Make a special Java C header by calling javah on your .c file:
2) Make a new JNI C file out of your original C file and include the JNI interface and your just-made javah header file. Export your functions to Java by modifying the function signature. Use JNI's special types (eg. jdbouble) for anything coming into your function from Java:
#include <jni.h> /* Java Native Interface headers */
#include "myJavaCFile.h" /* Auto-generated header created by javah -jni */
JNIEXPORT jdouble JNICALL
myJavaCFunction(JNIEnv *env, jobject obj, jdouble x)
{
double x = 0.0;
/* Call the original C function */
y = myOriginalCFunction(x);
return y;
}
3) Compile your file into a shareable lib (architecure specific)
1) declare your C function in Java using the native keyword. 'native' says: it'll come from a library later. Do this in a static block of your class:
static {
private native double myCFunction(double x);
}
2) Get the library and load it. Note that you may want to set the library path either in the call arguments (
System.loadLibrary("myLibName");
// or .dll or .dylib etc.
// System will check for right ending itself
3) Call it! Make a new object of your class, and use the function eg.
MyClass mc = new MyClass();
double myRtnValue = mc.myCFunction(2.33);
Tada.....C goodness for free!
For a more complete (and much better description!) of this process, see the java documentation from Sun at:
java.sun.com/docs/books/jni/html/jniTOC.html
For a link to a practical implementation of a bessel function:
www.nag.co.uk/doc/techrep/html/tr3_00/TRExample1.html
Bessel functions are solutions to Bessel's equation, and are useful for analysing wave propagation and decay in spherical or cylindrical coordinate systems: they are used extensively in signal processing and acoustics. Think drumskin vibrations varying over time. If you want to chart the y-displacement of the skin after your bang the drum, ask a Bessel function.
There are two major tasks to get your C stuff into Java:
A. Modify and compile your C file using JNI hooks:
1) Make a special Java C header by calling javah on your .c file:
javah myOriginalCFile.c -o myJavaCFile.h
2) Make a new JNI C file out of your original C file and include the JNI interface and your just-made javah header file. Export your functions to Java by modifying the function signature. Use JNI's special types (eg. jdbouble) for anything coming into your function from Java:
#include <jni.h> /* Java Native Interface headers */
#include "myJavaCFile.h" /* Auto-generated header created by javah -jni */
JNIEXPORT jdouble JNICALL
myJavaCFunction(JNIEnv *env, jobject obj, jdouble x)
{
double x = 0.0;
/* Call the original C function */
y = myOriginalCFunction(x);
return y;
}
3) Compile your file into a shareable lib (architecure specific)
B. Use your new .lib in Java - there are 3 steps:
1) declare your C function in Java using the native keyword. 'native' says: it'll come from a library later. Do this in a static block of your class:
static {
private native double myCFunction(double x);
}
2) Get the library and load it. Note that you may want to set the library path either in the call arguments (
-DLD_DYNAMIC_LIBRARY
) or call set_library_path
. Note that you don't need to include the file ending: Java will look for a .dll, .so, or .dylib depending on where you're creating your file.System.loadLibrary("myLibName");
// or .dll or .dylib etc.
// System will check for right ending itself
3) Call it! Make a new object of your class, and use the function eg.
MyClass mc = new MyClass();
double myRtnValue = mc.myCFunction(2.33);
Tada.....C goodness for free!
For a more complete (and much better description!) of this process, see the java documentation from Sun at:
java.sun.com/docs/books/jni/html/jniTOC.html
For a link to a practical implementation of a bessel function:
www.nag.co.uk/doc/techrep/html/tr3_00/TRExample1.html
Bessel functions are solutions to Bessel's equation, and are useful for analysing wave propagation and decay in spherical or cylindrical coordinate systems: they are used extensively in signal processing and acoustics. Think drumskin vibrations varying over time. If you want to chart the y-displacement of the skin after your bang the drum, ask a Bessel function.
Comments
Post a Comment