JavaTM cannot do it all. Sometimes you need to write system-specific code. This can be done use JNI (Java Native Interface). It allows your Java programs to use functions written in C. Here's the way I got it to work under Linux.
class Hello { public native void printGreeting(); static { System.loadLibrary("hello"); } public static void main(String[] args) { Hello hello = new Hello(); hello.printGreeting(); } }
#include "Hello.h" #include <stdio.h> JNIEXPORT void JNICALL Java_Hello_printGreeting(JNIEnv *env, jobject obj) { printf("Hello World\n"); }
gcc -o libhello.so -shared -Wl,-soname,libhello.so -I/usr/local/java/include -I/usr/local/java/include/linux Hello.c -static -lc