Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Unable to call the non-Static Java Method from C/C++

843829Dec 2 2007 — edited Dec 6 2007
Hi,

I am calling Java methods from C/C++ using JNI.

Now I can easily call the main method, but I am unable to call other non-static methods.

Signatures created by "javap -p -s HelloWorld" are given below:
-------------------------------------------------------------------------
javap -s -p HelloWorld
Compiled from "HelloWorld.java"
public class HelloWorld extends java.lang.Object{
public HelloWorld();
Signature: ()V
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
public void calling(java.lang.String);
Signature: (Ljava/lang/String;)V
}
---------------------------------------------------------------------------
C/C++ code is given below:
---------------------------------------------------------------------------
jclass cls = env->FindClass("HelloWorld");
if (cls != NULL)
{
jmethodID midMain = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if(midMain != NULL)
env->CallStaticVoidMethod(cls, midMain, NULL);

jmethodID midCalling = env->GetMethodID(cls,"calling","([Ljava/lang/String;)V");
if (midCalling!=NULL)
{
jstring StringArg = env->NewStringUTF("\nCalled from the C Program\n");
env->CallVoidMethod(cls,midCalling,StringArg);
}
}
---------------------------------------------------------------------------
Main method has not problem. It works fine but when I call the 2nd non-static method i.e. "calling", it fails.

If I change the signatures of the calling function from non-static to static it works fine.

Kindly help me to resolve this problem.

Regards,

Ahmad Jalil Qarshi
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2008
Added on Dec 2 2007
2 comments
190 views