69,368
社区成员
发帖
与我相关
我的任务
分享
class Prompt {
// native method that prints a prompt and reads a line
private native String getLine(String prompt);
public static void main(String args[]) {
Prompt p = new Prompt();
String input = p.getLine("Type a line: ");
System.out.println("User typed: " + input);
}
static {
System.loadLibrary("Prompt");
}
}
JNI文件代码示例:
XXX.c
JNIEXPORT jstring JNICALL
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
/* assume the prompt string and user input has less than 128 characters */
char outbuf[128], inbuf[128];
/* 下面的2句提取从Java那边传递过来的字符串到outbuf */
int len = (*env)->GetStringLength(env, prompt);
(*env)->GetStringUTFRegion(env, prompt, 0, len, outbuf);
printf("%s", outbuf);
scanf("%s", inbuf);
return (*env)->NewStringUTF(env, inbuf);
}
到网上搜索 JNI Programmer’s Guide and Specification 文档,有详细的阐述。
以前做过几个java结合JNI的案子,这份资料帮助很大,呵呵。userInfo.name = temName;//但是这么赋值好像不行
java的String对象存储了字符对象"hello",不想c语言的char*,只存储了一个指针,这样当然不行。
LZ要做的是提取String对象的内容,然后在JNI中分配一段同样大小的内存,然后存储该字符串。