Skip to main content

getting handle from a compiled java program

6 replies [Last post]
triadyk
Offline
Joined: 2007-04-12
Points: 0

Hi,
I'm just wondering if it's possible to get a handle of a GUI object of a compiled java program. What I would like to do is get the properties of a particular object (get the text value of a JTextField object, for example) from another application using java.

Ideally, I would like to be able to manipulate this 'foreign' GUI object as if it was a local object in my app.

Is this possible?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
meek
Offline
Joined: 2005-10-13
Points: 0

I am not sure ... but i guess you can use JNI to get some information ... Not sure

triadyk
Offline
Joined: 2007-04-12
Points: 0

As far as I know, JNI can be used to call native codes (DLLs) from java, not the other way around.

I've tried using Java Access Bridge and was able to get some properties of these controls as seen by the java ferret, but that's not good enough in my case... I want to get a handle of the java GUI object from another java program

meek
Offline
Joined: 2005-10-13
Points: 0

Well, in every JNI call ... you have an object parameter received in JNI function ... you can use this object to receive and function from the JAVA CODE any Field from Java code and execute java methods from jni.

I have done a project in which i sent call to jni code .. then in jni .. i used the methods to set/get java class properties and then fire an event (from the jni) using the java code.

triadyk
Offline
Joined: 2007-04-12
Points: 0

That sounds interesting. Would you care to explain more about this? Especially how you got the object reference and get access to that object's methods. In my case, I only have the compiled application and doesn't have access to the source (and doesn't know what methods are available for that particular object).

An example would be great too (codes speaks louder than a thousand words?)

My experience with JNI has only been around jawin, which was used to call functions from DLLs, so this is a totally new experience for me.

Thanks.

meek
Offline
Joined: 2005-10-13
Points: 0

ok, i don't have exact code and exact scenario but i can write a simple explanation demo for you.

Suppose you have a x number of methods (setter methods) which sets the field of some variables of the class ... Now, you call you JNI code from that class and that JNI Code (C code) need to set the some of the fields of the class. One possible solution is to call the setter method.

say .. the java code for setters are
[code]
String fieldOne;

public void setFieldOne (String fieldOne){
this.fieldOne = fieldOne;
}
[/code]

Now you can set this field either by direct calling to this field or directly calling the method of this class.

code for jni
[code]

/* Directly setting the field
For more detail please read JNI tutorial ...
*/
void setStringField (JNIEnv *env, jobject obj, const char *fieldName, jstring fieldVal)
{
jfieldID fid;

jclass cls = env ->GetObjectClass(obj);
fid = env ->GetFieldID(cls, fieldName, "Ljava/lang/String;");

env -> SetObjectField(obj, fid, fieldVal);
}
[/code]

and here is the code for calling a method which returns nothing and accept one string as arguement

[code]
void voidMethodArgOneString (JNIEnv * env, jobject obj, const char * methodName, const char * arg)
{
jclass cls = env ->GetObjectClass(obj);
jmethodID mid = env ->GetMethodID(cls, methodName, "(Ljava/lang/String;)V");
env ->CallVoidMethod(obj, mid, env->NewStringUTF(arg));

}
[/code]

If you have enough idea about reflection then you might get no problem understanding this code. Again, my this c file was called from java and in this C file i am calling and using either java class fields or it's methods. There is a complete chapter on method signatures, and retriving a method. Read out the JNI tutorial if you don't have one .. then give me your mail ID ... i'll mail you the eBook i used to learn this :)

Cheers,

triadyk
Offline
Joined: 2007-04-12
Points: 0

looks like there's no way to avoid using C as the "middle man" after all...

thanks meek! that was helpful. And yes, please do send me the ebook. My email is <..deleted..>