#include<jni.h> #include<stdio.h> #include"BTest.h" #include<cstring> /* *Class: BTest *Method: hello *Signature: ()V */ JNIEXPORT jstring JNICALL Java_BTest_hello (JNIEnv* env, jobject){ const char *pat = "hello"; jclass strClass = (env)->FindClass("java/lang/String"); jmethodID ctorID = (env)->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); jbyteArray bytes = (env)->NewByteArray(strlen(pat)); (env)->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte *) pat); jstring encoding = (env)->NewStringUTF("utf-8"); return (jstring) (env)->NewObject(strClass, ctorID, bytes, encoding); }
转成jstring类型return,java端调用的时候可以用String类型接收参数