S
Stephen Kellett
Hi folks,
I've called Java methods in classes of my own creation from C without
trouble. However I have a job where I need to do the following from C.
Here is the Java that would do the job.
long value;
value = java.lang.Runtime.getRuntime().getFreeMemory();
I can get the class "java.lang.Runtime", however when I then try to get
the method id for the static method getRuntime() it fails (returns with
a 0 method id).
Same when I try to get the non-static method "getFreeMemory()" it fails.
Any ideas?
JNIEnv *Env // assume this is valid (in my code it is checked and
valid)
jlong value = 0;
jclass jc = NULL;
jmethodID jmid = NULL;
jc = Env->FindClass("java.lang.Runtime");
if (jc != NULL)
{
jobject runtime;
jmid = Env->GetStaticMethodID(jc,
"getRuntime",
"()Ljava.lang.Runtime;");
if (jmid != NULL)
{
runtime = Env->CallStaticObjectMethod(jc, jmid);
if (runtime != NULL)
{
jmid = Env->GetMethodID(jc,
methodName,
methodSignature);
if (jmid != NULL)
{
value = Env->CallLongMethod(runtime,
jmid);
}
}
}
}
Alternatively, if someone knows how to get the memory stats in C without
doing the above that would be cool.
Stephen
I've called Java methods in classes of my own creation from C without
trouble. However I have a job where I need to do the following from C.
Here is the Java that would do the job.
long value;
value = java.lang.Runtime.getRuntime().getFreeMemory();
I can get the class "java.lang.Runtime", however when I then try to get
the method id for the static method getRuntime() it fails (returns with
a 0 method id).
Same when I try to get the non-static method "getFreeMemory()" it fails.
Any ideas?
JNIEnv *Env // assume this is valid (in my code it is checked and
valid)
jlong value = 0;
jclass jc = NULL;
jmethodID jmid = NULL;
jc = Env->FindClass("java.lang.Runtime");
if (jc != NULL)
{
jobject runtime;
jmid = Env->GetStaticMethodID(jc,
"getRuntime",
"()Ljava.lang.Runtime;");
if (jmid != NULL)
{
runtime = Env->CallStaticObjectMethod(jc, jmid);
if (runtime != NULL)
{
jmid = Env->GetMethodID(jc,
methodName,
methodSignature);
if (jmid != NULL)
{
value = Env->CallLongMethod(runtime,
jmid);
}
}
}
}
Alternatively, if someone knows how to get the memory stats in C without
doing the above that would be cool.
Stephen