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!

helps need in invoking JVM from C++ and accessing fields

843829Aug 28 2005 — edited Sep 16 2005
Hi,

I wrote the program to create JVM from C++, passing a string to java program to java and tried to get /set the field. I I have the problems in accessing the fields

here is the java and c++ codes

public class ApiTest {

private int i = 1;
private String txt = " ";
private init ( String str) {
i = 2;
txt = str;
}

public static void main(String[] args) {
ApiTest api = new ApiTest ( ) ;
api.init ( args [0]);
System.out.println( args[0]);
System.out. println ( "i =" + i);
System.out.println ( "txt = " + txt);
}
}


C++ codes like this

1. create JVM
2. find class
jcls = (*env)->findCalss (env, " ApiTest")
3. call class method ID
4. invoke the method with args string , it was converted to jstring
5. getFieldID of Int and String
6. getIntField
7, set new value back to Int field
8. get string field


up to step 5 , it looks work okay it can pass the string as args [0] to java
invoke the init (), but problems are
1. I can get the int and print out but value is not 1
2, after I set the new value then I get it again, it the same value , nothing changed
3. I can't get jstring in string field , it shows that can't get jstring at all
jstring jst = (*env)->getObjectField (env, jcls, jfid2)
then jstring==0

what is the problem? I mess up about jclass and jobject I think, but I don't know how to get class object after invoke the main method.

thank you for any help
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 14 2005
Added on Aug 28 2005
25 comments
700 views