Sample code for 30+ languages & platforms
Java

Call a JavaScript Function Returning an Integer

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an integer.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    // ----------------------------------------------------------------------------------
    // The Javascript function called in this example is shown at the bottom of this page.
    // -----------------------------------------------------------------------------------

    // In this example, we'll load the Javascript function definition from a file.
    // It doesn't need to come from a file.  It could just as easily be loaded from a string.
    CkStringBuilder sbScript = new CkStringBuilder();
    success = sbScript.LoadFile("js_call_function.js","utf-8");
    if (success == false) {
        System.out.println(sbScript.lastErrorText());
        return;
        }

    // Note: Each instance of a Chilkat Js object automatically establishes
    // its own internal runtime and context.  Applications do not need to explicitly create
    // the JavaScript runtime or context.
    CkJs js = new CkJs();

    CkJsonObject result = new CkJsonObject();
    result.put_EmitCompact(false);

    // Call Eval to add the function (shown at the bottom of this page) to the context's global object
    success = js.Eval(sbScript,result);
    if (success == false) {
        // Examine the result for an exception.
        System.out.println(result.emit());

        // Also examine the LastErrorText.
        System.out.println(js.lastErrorText());
        return;
        }

    System.out.println(result.emit());

    // The expected output is "undefined":

    // {
    //   "type": "undefined",
    //   "value": "undefined"
    // }

    //    When Eval processes a script containing only a function declaration,
    //    it successfully performs the action (the function becomes defined).
    //    However, since the script consists of a statement that produces no value,
    //    the script's overall completion value is empty. In JavaScript, the
    //    absence of a value is represented by `undefined`.
    // 
    //    Therefore, the Eval call returns `undefined`.

    // ------------------------------------------------------------------------------
    // Call the function calculateScore("Player1", 10, 20)

    CkJsonObject funcCall = new CkJsonObject();

    // Create JSON defining the function call:

    // {
    //   "name": "calculateScore",
    //   "args": [ "Player1", 10, 20 ]
    // }

    funcCall.UpdateString("name","calculateScore");
    funcCall.UpdateString("args[0]","Player1");
    funcCall.UpdateInt("args[1]",10);
    funcCall.UpdateInt("args[2]",20);

    funcCall.put_EmitCompact(false);
    System.out.println(funcCall.emit());

    success = js.CallFunction(funcCall,result);
    if (success == false) {
        // Examine the result for an exception.
        System.out.println(result.emit());

        // Also examine the LastErrorText.
        System.out.println(js.lastErrorText());
        return;
        }

    System.out.println(result.emit());

    // Output:

    // {
    //   "type": "int",
    //   "value": 37
    // }

    int retval = result.IntOf("value");
    System.out.println("retval = " + retval);
  }
}