The CALLJ statement allows a jBC program to call a Java method. CALLJ is useful when using third party applications offering a Java API (for example, publish and subscribe, messaging, etc.)
CALLJ packageAndClassName, [$]methodName, param SETTING ret [ON ERROR] statements
| packageAndClassName | The “full” class name (e.g., com.jbase.util.utilClass). |
| methodName | The name of the Java method in this class (e,g., “myMethod”). If the method is static, you must include a ‘$’ before the name. This ‘$’ will be removed from the method name before calling it. |
| param | Any parameter (eg DynArray) |
The Java virtual machine is loaded dynamically at runtime, so a compiled jBC application has no dependencies on any Java virtual machine. By default, the program will search for:
Although it is not usually necessary, it is possible to specify a Java library by setting the JBCJVMLIB environment variable:
set JBCJVMLIB= C:\jdk1.3.1\jre\bin\hotspot\jvm.dll
Any errors which occur during the call can be managed at the jBC level by getting the SYSTEM(0) variable.
This variable can have the following values:
In Java:
package mypackage;
public class mytestclass {
static int i = 0;
private mytestclass() {
}
public String mymethod(String s){
return (“Java Received : “ + s) ;
}
public static String mystaticmethod(String s){
i++;
return s + " " + i;
}
}
To call these methods from jBC:
CALLJ "mypackage.mytestclass","mymethod", p SETTING ret
CRT ret
CALLJ "mypackage/mytestclass","$mystaticmethod",p SETTING ret
CRT ret
jBC code using the ON ERROR error handling:
PROGRAM testcallj
CRT "Please enter a Class Name":
INPUT className
CRT "Please enter a Method Name":
INPUT methodName
CRT "Is this a static method (y/n)":
INPUT static
IF UPCASE(static) = "Y" THEN methodName = "$" : methodName
CRT "Please enter a Parameter":
INPUT param
CALLJ className,methodName, param SETTING ret ON ERROR
GOSUB errorHandler
STOP
END
CRT "Received from Java: " : ret
STOP
errorHandler:
err = SYSTEM(0)
BEGIN CASE
CASE err = 1
CRT "Fatal Error creating Thread!"
CASE err = 2
CRT "Cannot find the JVM.dll !"
CASE err = 3
CRT "Class " : className : " doesn't exist!"
CASE err = 4
CRT "UNICODE conversion error!"
CASE err = 5
CRT "Method " : methodName : " doesn't exist!"
CASE err = 6
CRT "Cannot find object Constructor!"
CASE err = 7
CRT "Cannot instantiate object!"
CASE @TRUE
CRT "Unknown error!"
END CASE
RETURN
JBC