#include <jni.h>
#include "com_test_Test.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
JNIEXPORT void JNICALL Java_com_test_Test_sayHello(JNIEnv * env, jobject obj,
jstring s) {
char * str = (char *) (*env)->GetStringUTFChars(env, s, 0);
printf("\n c-string: hello - %s", str);
}
JNIEXPORT jint JNICALL Java_com_test_Test_add(JNIEnv * env, jobject obj,
jint a, jint b) {
int i = a;
int j = b;
printf("\n c-int: %d - %d", i, j);
char str[256];
sprintf(str, "%d", i + j);
return (jint) i + j;
}
JNIEXPORT jfloat JNICALL Java_com_test_Test_getFloat(JNIEnv * env, jobject obj,
jfloat f) {
float fl = f;
printf("\n c-float: %3f", fl);
fl = 200.555;
return (jfloat) fl;
}
JNIEXPORT jdouble JNICALL Java_com_test_Test_getDouble(JNIEnv * env,
jobject obj, jdouble dou) {
double d = dou;
printf("\n c-double: %3f", d);
d = 800.88;
return (jdouble) d;
}
JNIEXPORT jboolean JNICALL Java_com_test_Test_getBoolean(JNIEnv * env,
jobject obj, jboolean bool) {
unsigned char b = bool;
printf("\n c-boolean: %lu ", b);
if (b) {
printf("true");
} else {
printf("false");
}
b = 1;
return (jboolean) b;
}
JNIEXPORT jstring JNICALL Java_com_test_Test_getString(JNIEnv * env,
jobject obj, jstring s) {
char * st = (char *) (*env)->GetStringUTFChars(env, s, 0);
printf("\n c-string: %s", st);
char * str = "hello wang wu!";
jstring rtn;
rtn = (*env)->NewStringUTF(env, str);
return rtn;
}
JNIEXPORT jobjectArray JNICALL Java_com_test_Test_getStringArray(JNIEnv * env,
jobject obj, jobjectArray arr) {
int len = (*env)->GetArrayLength(env, arr);
printf("\n c-stringArray :");
int i = 0;
for (i = 0; i < len; i++) {
jobject obj = (*env)->GetObjectArrayElement(env, arr, i);
jstring str = (jstring) obj;
const char * szStr = (*env)->GetStringUTFChars(env, str, 0);
printf(" %d-%s ", i, szStr);
(*env)->ReleaseStringChars(env, str, szStr);
}
jstring str;
jobjectArray args = 0;
jsize size = 5;
char* sa[] = { "Hello,", "world!", "zhang", "san", "yuang" };
int j = 0;
jclass objClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, size, objClass, 0);
for (j = 0; j < size; j++) {
str = (*env)->NewStringUTF(env, sa[j]);
(*env)->SetObjectArrayElement(env, args, j, str);
}
return args;
}