Name: rm29839 Date: 11/25/97
Simply use the HelloWorld example of JNI. Modify
the HelloWorld class and HelloWorldImp.c a little.
Here is the java class:
class HelloWorld {
protected int number;
protected double value;
public HelloWorld()
{
number = 1;
value = 30.0;
}
public int getNumberV()
{
return number;
}
public int getNumbetI(int i)
{
return number;
}
public double getValueV()
{
return value;
}
public double getValueI(int i)
{
return value;
}
public native void displayHelloWorld();
static {
System.loadLibrary("hello");
}
}
This is the native code:
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid;
mid = (*env)->GetMethodID(env, cls, "getNumberV", "(V)I");
if(mid == 0)
printf("GetMethodID(...,\"(V)I\") failed.\n");
mid = (*env)->GetMethodID(env, cls, "getNumberI", "(I)I");
if(mid == 0)
printf("GetMethodID\(...,\"(I)I\") failed.\n");
mid = (*env)->GetMethodID(env, cls, "getValueV", "(V)D");
if(mid == 0)
printf("GetMethodID\(...,\"(V)D\") failed.\n");
mid = (*env)->GetMethodID(env, cls, "getValueI", "(I)D");
if(mid == 0)
printf("GetMethodID\(...,\"(I)D\") failed.\n");
printf("Hello world!\n");
return;
}
This is the run-time result:
GetMethodID(...,"(V)I") failed.
GetMethodID(...,"(I)I") failed.
GetMethodID(...,"(V)D") failed.
Hello world!
(Review ID: 20708)
======================================================================