Skip to content

Jexl script

pranithcodes edited this page Apr 6, 2018 · 3 revisions

A script is some valid JEXL syntax to be executed with a given set of JexlContext variables. A script is a group of statements, separated by semicolons.. Scripts can be read from a String, File or URL.

A statement can be the empty statement, the semicolon (;) , block, assignment, control statements or an expression. Statements are optionally terminated with a semicolon.

A block is simply multiple statements inside curly braces ({, }).

Using the return keyword, a script will return the expression that follows (or null). A script returns the last expression evaluated by default.

JexlScripts can be exeucted or evaluted by JexlScriptEngine.

Finally, JEXLEnigne is used evalute JEXL expressions where as JEXLScriptEngine for evaluating the JEXL script.

Below is a simple example to execute multiple statements:

public static void main(String[] args) throws ScriptException {

	String script = " X = X + 2 ; Y = Y + 5 ; Z = X+Y";
	JexlScriptEngine engine = new JexlScriptEngine();
	engine.put("X", 2);
	engine.put("Y", 15);

	engine.eval(script);

	System.out.println("The value of Z ::" + engine.get("Z"));

}