home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 15
/
BUGCD1998_06.ISO
/
aplic
/
jbuilder
/
jsamples.z
/
NativeExampleImpl.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-07-17
|
2KB
|
66 lines
/*
* This file contains the implementation of the native methods, that
* are declared in class NativeExample.
*/
#include "jni.h"
#include <stdio.h>
/*
* Display a Java string on the console
*/
#pragma argsused
void JNICALL JNIEXPORT Java_NativeExample_Display(
JNIEnv *env,
jclass cl,
jstring str)
{
const char *buf = (*env)->GetStringUTFChars(env, str, 0);
printf(buf);
(*env)->ReleaseStringUTFChars(env, str, buf);
return;
}
/*
* A simple calculation, using the long integer macros
*/
#pragma argsused
jlong JNICALL JNIEXPORT Java_NativeExample_Sub(
JNIEnv *env,
jclass cl,
jlong a,
jlong b)
{
return ll_sub(a, b);
}
/*
* This method shows how to call a Java method from native
* code. Method int Fib(int) is defined in class Fibonacci.
*/
#pragma argsused
void JNICALL JNIEXPORT Java_NativeExample_Fibonacci(
JNIEnv *env,
jclass cl,
jint fib_arg)
{
jclass fib_cl;
jmethodID fib_id;
jint fib_result;
/* lookup the Fibonacci class */
fib_cl = (*env)->FindClass(env, "Fibonacci");
if (fib_cl)
{
fib_id = (*env)->GetStaticMethodID(env, fib_cl, "Fib", "(I)I");
if(fib_id)
{
/* call a java method from native code */
fib_result = (*env)->CallStaticIntMethod(env, fib_cl, fib_id, fib_arg);
printf("Fib(%d)=%d\n", fib_arg, fib_result);
}
else
printf("In class Fibonacci: int Fib(int) not defined\n");
}
else
printf("class not found\n");
}