Trivial JNI example
public class nativetest
{
public native String sayHello(String s);
public static void main(String[] argv)
{
String retval = null;
nativetest nt = new nativetest();
retval = nt.sayHello("Beavis");
System.out.println("Invocation returned " + retval);
}
}
/* DO NOT EDIT THIS FILE - it is machine generated */ #include/* Header for class nativetest */ #ifndef _Included_nativetest #define _Included_nativetest #ifdef __cplusplus extern "C" { #endif /* * Class: nativetest * Method: sayHello * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_nativetest_sayHello (JNIEnv *, jobject, jstring); #ifdef __cplusplus } #endif #endif
#include "nativetest.h" /*double quotes tells it to search current directory*/
JNIEXPORT jstring JNICALL Java_nativetest_sayHello
(JNIEnv *env, jobject thisobject, jstring js)
{
return js;
}
cl -c /Ic:\jdk1.1.6\include /Ic:\jdk1.1.6\include\win32 nativetest.c link /libpath=c:\jdk1.1.6\lib nativetest.obj /dllThe above uses Microsoft Visual C++ command-line tools.
public class nativetest
{
static {
System.loadLibrary("nativetest");
}
public native String sayHello(String s);
public static void main(String[] argv)
{
String retval = null;
nativetest nt = new nativetest();
retval = nt.sayHello("Beavis");
System.out.println("Invocation returned " + retval);
}
}
C:\jni\hello>java nativetest Invocation returned Beavis