CALLJ


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.)

 

COMMAND SYNTAX

CALLJ packageAndClassName, [$]methodName, param SETTING ret [ON ERROR] statements

 

SYNTAX ELEMENTS

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)

 

NOTES

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:

  • jvm.dll on Windows platforms
  • libjvm.sl on HP Unix
  • libjvm.so for other platforms

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:

  1. Fatal error creating thread
  2. Cannot create JVM
  3. Cannot find class
  4. Unicode conversion error
  5. Cannot find method
  6. Cannot find object constructor
  7. Cannot instantiate object

 

PERFORMANCE CONSIDERATIONS

The first call to CALLJ carries the overhead of loading the Java Virtual Machine into memory.

Susequent calls do not have this overhead and it is recommended that programs are structured in such a way that the Java Virtual Machine is only loaded once.

In addition, calls to non static methods carry the overhead of calling the constructor for the class. Wherever possible, static methods should be used.

 

 

EXAMPLE

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