学习 native 的使用
从java2 参考大全上拷贝了几段代码。
通过javah -jni NativeDemo生成了下面的文件。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class NativeDemo */
#ifndef _Included_NativeDemo
#define _Included_NativeDemo
#ifdef __cplusplus
extern "C " {
#endif
/*
* Class: NativeDemo
* Method: test
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_NativeDemo_test
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
又拷贝了个NativeDemo.c
#include "NativeDemo.h "
JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *env, jobject obj)
{
jclass cls;
jfieldID fid;
jint i;
printf( "Starting the native method.\n ");
cls = (*env)-> GetObjectClass(env, obj);
fid = (*env)-> GetFieldID(env, cls, "i ", "I ");
if(fid == 0) {
printf( "Could not get field id.\n ");
return;
}
i = (*env)-> GetIntField(env, obj, fid);
printf( "i = %d\n ", i);
(*env)-> SetIntField(env, obj, fid, 2*i);
printf( "Ending the native method.\n ");
}
接下来要做的事情就是把 .c文件编译成.dll文件。
从网上下载了个dev c++
把javahome下面的include全部拷贝到dev的include里面去了,包含(win32文件夹里面的)。
但是编译NativeDemo.C的时候,出现了下面的错误。
C:\Dev-Cpp\NativeDemo.c
[Warning] In function `void Java_NativeDemo_test(JNIEnv*, _jobject*) ':
13 C:\Dev-Cpp\NativeDemo.c
base operand of `-> ' has non-pointer type `JNIEnv_ '
14 C:\Dev-Cpp\NativeDemo.c
base operand of `-> ' has non-pointer type `JNIEnv_ '
19 C:\Dev-Cpp\NativeDemo.c
base operand of `-> ' has non-pointer type `JNIEnv_ '
21 C:\Dev-Cpp\NativeDemo.c
base operand of `-> ' has non-pointer type `JNIEnv_ '
C:\Dev-Cpp\Makefile.win
[Build Error] [NativeDemo.o] Error 1
问题:1 上面的错误什么意思?
2 是不是编译一下就能生成 NativeDemo.dll了?
就10分了,望高人指点。
------解决方案--------------------
(*env)-> GetObjectClass()
这是搞啥
env-> GetObjectClass()不就行了