home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / NativeExampleImpl.c < prev    next >
C/C++ Source or Header  |  1998-05-08  |  2KB  |  68 lines

  1. // $Header: z:/admin/jb20_samples.English/borland/samples/NativeExample/jni/rcs/NativeExampleImpl.c 1.2 1998/03/20 19:14:12 CDAWSON Exp $ 
  2. /*
  3.  * This file contains the implementation of the native methods, that
  4.  * are declared in class NativeExample.
  5.  */
  6. #include "jni.h"
  7. #include "jni_md.h"
  8. #include <stdio.h>
  9. /*
  10.  * Display a Java string on the console
  11.  */
  12. #pragma argsused
  13. void JNICALL JNIEXPORT Java_NativeExample_Display(
  14.     JNIEnv     *env,
  15.     jclass    cl,
  16.     jstring     str)
  17. {
  18.     const char *buf = (*env)->GetStringUTFChars(env, str, 0);
  19.     printf(buf);
  20.     (*env)->ReleaseStringUTFChars(env, str, buf);
  21.     return;
  22. }
  23.  
  24. /*
  25.  * A simple calculation, using the long integer macros
  26.  */
  27. #pragma argsused
  28. jlong JNICALL JNIEXPORT Java_NativeExample_Sub(
  29.     JNIEnv    *env,
  30.     jclass    cl,
  31.     jlong    a,
  32.     jlong    b)
  33. {
  34.     return ll_sub(a, b);
  35. }
  36.  
  37. /*
  38.  * This method shows how to call a Java method from native
  39.  * code. Method int Fib(int) is defined in class Fibonacci.
  40.  */
  41. #pragma argsused
  42. void JNICALL JNIEXPORT Java_NativeExample_Fibonacci(
  43.     JNIEnv    *env,
  44.     jclass    cl,
  45.     jint    fib_arg)
  46. {
  47.     jclass    fib_cl;
  48.     jmethodID    fib_id;
  49.     jint    fib_result;
  50.  
  51.     /* lookup the Fibonacci class */
  52.     fib_cl = (*env)->FindClass(env, "Fibonacci");
  53.     if (fib_cl)
  54.     {
  55.     fib_id = (*env)->GetStaticMethodID(env, fib_cl, "Fib", "(I)I");
  56.     if(fib_id)
  57.     {
  58.         /* call a java method from native code */
  59.         fib_result = (*env)->CallStaticIntMethod(env, fib_cl, fib_id, fib_arg);
  60.         printf("Fib(%d)=%d\n", fib_arg, fib_result);
  61.     }
  62.     else
  63.         printf("In class Fibonacci: int Fib(int) not defined\n");
  64.     }
  65.     else
  66.         printf("class not found\n");
  67. }
  68.