Using the Raw Native Interface Previous
Previous
Introduction
Introduction
Next
Next

Calling Java Methods

Common , Repeated Calls

These sections illustrate how to call Java methods.


Common

The most common method for calling a Java API from native code is by using execute_java_dynamic_method(), for example to call "move" on a Rectangle object:

    execute_java_dynamic_method(NULL, phRectangle, "move", "(II)V", x, y);

The first parameter should always be NULL, the second is the object who's method you want to call, the third is the name of the method, the fourth is its signature, finally you pass the parameters. The signature specifies the types of the parameters surrounded by parenthesis followed by the return type. In this case, "(II)V" translates to "param 1 is an integer, param 2 is an integer and the return type is void".

Here's a table of how each type maps to it's signature character.
Parameter Signature Char
array [
boolean Z
byte B
char C
double D
float F
int I
long J
object L
short S
void V

For arrays, the signature char is followed the signature char of the type of the array, for example an array of bytes would be "[B".

For objects, the signature is followed by the object class name and ends with a ';'. For example, the signature for a Rectangle is "Ljava/awt/Rectangle;"

To call a static method you can use execute_java_static_method() for example, to call System.gc() you would write:

    ClassClass *pcc = FindClass(NULL, "java/lang/System", FALSE); 
    execute_java_static_method(NULL, pcc, "gc", "()V");

FindClass() is used to get a pointer to the class object, the first parameter should be NULL, the second is the class name and the third should be FALSE. Given a class pointer you then call execute_java_static_method(), the first parameter should be NULL, the second is the class pointer the third is the method name, the fourth is the signature and finally you would pass the arguments but in this case there aren't any.


Repeated Calls

For performing repeated calls to the same method of a particular object you can use the more low-level do_execute_java_method() API to avoid repeated name-lookups. For example, to "move" on a Rectangle 10 times:

    struct methodblock *pmb = get_methodblock(phRectangle, "move", "(II)V");

    for (i = 0; i < 10; i++)
    {
        do_execute_java_method(NULL, phRectangle, NULL, NULL, pmb, FALSE, x, y);
    }

First, get_methodblock() is used to get a pointer to a cached data structure representing the object and the method. The first parameter is the object pointer, the second is the method name and the third is the method signature. Given a methodblock pointer you then call do_execute_java_method(), the first parameter should be NULL, the second is the object pointer, the third and fourth parameters should also be null, the fifth should be TRUE for a static call and FALSE for a non-static call and finally you pass the arguments.

Top© 1996 Microsoft Corporation. All rights reserved.