HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Understanding the Java Native Interface"
Vol. 2, Issue 3, p. 12

	

Listing 1: NativeQuote.java

public class NativeQuote {
	public native void quote();

	static {
		System.loadLibrary("nativequote");
	}

	public static void main(String[] args) {
		new NativeQuote().quote();
	}
	
}

Listing 2: NativeQuote.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class NativeQuote */

#ifndef _Included_NativeQuote
#define _Included_NativeQuote
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     NativeQuote
 * Method:    quote
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_NativeQuote_quote
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Listing 3: marvinQuote.c

#include "NativeQuote.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_NativeQuote_quote(JNIEnv *env, 
jobject this) {
	printf("Where's the kaboom? There was supposed to be \
        an earth-shattering kaboom! -- Marvin the Martian\n);
	return;
}

Listing 4: Factorial.java

public class Factorial {
	// method to recursively compute factorial
	//e.g. factorial(3) = 3 * 2 * 1 = 6
	public double factorial (int x) {
		if (x > 1) 
			return ((double) factorial(x-1) * x);
		else
			return ((double) 1);
	}
	
  	//native method which invokes above method
	public native void computeFact();

	static {
		System.loadLibrary("fact");
	}

	public static void main(String[] args) {
		new Factorial().computeFact();
	}
}

Listing 5: callFactorial.c

#include "Factorial.h"
#include <stdio.h>

JNIEXPORT void  JNICALL Java_Factorial_computeFact(JNIENV *env, jobject this)  {
	jdouble res;
	jint x ;
	jclass cls = (*env)->GetObjectClass(env,this);
	jmethodID mid = 
(*env)->GetMethodID(env,cls,"factorial","(I)D");
	if (mid ==0) return;
	x =5;
	res = (*env)->CallDoubleMethod(env,this,mid,x);
	printf("Factorial(%d) = %.0f\n",x,res);
}

Listing 6: FaveDrink.java

public class FaveDrink {
	public String beverage = "Café Au Lait";

	public native void changeDrink();

	static {
		System.loadLibrary("favedrink");
	}	
	
	public static void main(String[] args) {
		FaveDrink aDrink = new FaveDrink();

		System.out.println("Calling native method...\n");		 
		aDrink.changeDrink();
System.out.println("Back from native method 
    call...\n");
System.out.println("My favorite drink is now "+ 
    beverage);	
	}
}
   
Listing 7 alterDrink.c

#include "FaveDrink.h" 
#include <stdio.h>

JNIEXPORT void JNICALL Java_FaveDrink_changeDrink (JNIEnv *env, jobject this){

	jfieldID fid;
	jstring jstr;
	const char *drink;

	//obtain the class to which the current object belongs to	
	jclass cls = (*env)->GetObjectClass(env,this);

	//retrieve the fieldID for the Java field which 
	//you want to access
	fid =(*env)->GetFieldID(env,cls,"beverage",
                             "Ljava/lang/String;");
	//retrieve the value of the field using the appropriate 
	//accessor function
	
jstr = (*env)->GetObjectField(env,this,fid);
	drink = (*env)->GetStringUTFChars(env,jstr,0);
	printf("My favorite drink currently is %s \n",drink);

	printf("Hmm...not strong enough...");

	jstr = (*env)->NewStringUTF(env,"Turkish Coffee");
	//reset the value of the Java field using the appropriate
	//accessor function
	(*env)->SetObjectField(env,this,fid,jstr);
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.