This example demonstrates how to access the Java object from JavaScript statements. Java support the JavaScript access with the ScriptEngineManager class and ScriptEngine interface. Here you will see how we can execute the JavaScript code using the Java API’s.
- Create ScriptEngineManager which tracks the states of all the script engines.
- Create ScriptEngine by using the method getEngineByExtension() define in ScriptEngineManager. Here we pass the parameter “js” to get the JavaScript engine.
- Add the Java object which is used inside the script
- Evaluate the JavaScript by using the ScriptEngine.eval() method
Lets look at the example.
ScriptEngineExample.java
package javabeat.net.script; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; /** * Invoke Java from JavaScript example. * * @author Krishna * */ public class ScriptEngineExample { public static void main(String[] args) { // Create Java array with list of cities String cities[] = { "London", "NewYork", "Sydney", "Bangalore", "Chennai", "Mumbai" }; // Create script which accessing Java object String script = "var index; " + "var cities = citiesArray;" + "" + "for (index in cities) { " + "println(cities[index]);" + "}"; // Create ScriptEngine ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = engineManager.getEngineByExtension("js"); // Add Java object to script engine engine.put("citiesArray", cities); try { // Evaluate script using script engine engine.eval(script); } catch (ScriptException exception) { exception.printStackTrace(); } } }
Output…
London NewYork Sydney Bangalore Chennai Mumbai
javax.script.ScriptException
If you miss any syntax in the script, you would get the following exception.
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ( after for (<Unknown source>#1) in <Unknown source> at line number 1 at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source) at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source) at javax.script.AbstractScriptEngine.eval(Unknown Source) at javabeat.net.script.ScriptEngineExample.main(ScriptEngineExample.java:31) Caused by: sun.org.mozilla.javascript.internal.EvaluatorException: missing ( after for (<Unknown source>#1) at sun.org.mozilla.javascript.internal.DefaultErrorReporter.runtimeError(Unknown Source) at sun.org.mozilla.javascript.internal.DefaultErrorReporter.error(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.addError(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.addError(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.reportError(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.reportError(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.reportError(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.forLoop(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.statementHelper(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.statement(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.parse(Unknown Source) at sun.org.mozilla.javascript.internal.Parser.parse(Unknown Source) at sun.org.mozilla.javascript.internal.Context.compileImpl(Unknown Source) at sun.org.mozilla.javascript.internal.Context.compileReader(Unknown Source) at sun.org.mozilla.javascript.internal.Context.compileReader(Unknown Source) at sun.org.mozilla.javascript.internal.Context.evaluateReader(Unknown Source) ... 4 more