Java Requires Chilkat v11.4.0+
Chilkat2 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
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Js;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// ----------------------------------------------------------------------------------
// 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.
StringBuilder sbScript = new StringBuilder();
try {
sbScript.loadFile("js_call_function.js", "utf-8");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
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.
Js js = new Js();
JsonObject result = new JsonObject();
result.setEmitCompact(false);
// Call Eval to add the function (shown at the bottom of this page) to the context's global object
try {
js.eval(sbScript, result);
} catch (ChilkatException e) {
// Examine the result for an exception.
System.out.println(result.emit());
// Also examine the LastErrorText.
System.out.println(e.getLastErrorText());
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)
JsonObject funcCall = new JsonObject();
// 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.setEmitCompact(false);
System.out.println(funcCall.emit());
try {
js.callFunction(funcCall, result);
} catch (ChilkatException e) {
// Examine the result for an exception.
System.out.println(result.emit());
// Also examine the LastErrorText.
System.out.println(e.getLastErrorText());
return;
}
System.out.println(result.emit());
// Output:
// {
// "type": "int",
// "value": 37
// }
int retval = result.intOf("value");
System.out.println("retval = " + retval);
}
}
JavaScript
function calculateScore(name, pointsA, pointsB) {
// Return length of name + both scores
return name.length + pointsA + pointsB;
}