Calling java.lang.Runtime methods from C - help

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
 
G

Gordon Beaton

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.
[...]

jc = Env->FindClass("java.lang.Runtime");
if (jc != NULL)
{
jobject runtime;

jmid = Env->GetStaticMethodID(jc,
"getRuntime",
"()Ljava.lang.Runtime;");

The most common reason for not finding method or field ids is an
incorrect signature. Use javap -s <classname> to check!

This is the correct way to express it:

"()Ljava/lang/Runtime;"

/gordon
 
S

Stephen Kellett

Gordon Beaton <[email protected]> said:
The most common reason for not finding method or field ids is an
incorrect signature. Use javap -s <classname> to check!

This is the correct way to express it:

"()Ljava/lang/Runtime;"

Doh! Thank you very much. Bangs head against wall.

Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top