Catch ScriptException : Script Engines « JDK 6 « Java






Catch ScriptException

     


import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class BindingDemo {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 1);
    engine.put("b", 5);

    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object a = bindings.get("a");
    Object b = bindings.get("b");
    System.out.println("a = " + a);
    System.out.println("b = " + b);

    Object result;
    try {
      result = engine.eval("c = aaaa + bbbb;");
      System.out.println("a + b = " + result);
    } catch (ScriptException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}
/*a = 1
b = 5
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "aaaa" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
  at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
  at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
  at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
  at BindingDemo.main(BindingDemo.java:22)
*/

          
    
    
    
  








Related examples in the same category

1.Listing All Script Engines
2.Running Scripts with Java Script Engine
3.Execute Javascript script in a file
4.Variables bound through ScriptEngine
5.Any script have to be compiled into intermediate code.
6.With Compilable interface you store the intermediate code of an entire script
7.List the script engines
8.Run JavaScript and get the result by using Java
9.Pass parameter to JavaScript through Java code
10.Get the value in JavaScript from Java Code by reference the variable name
11.Working with Compilable Scripts
12.Call a JavaScript function three times
13.Save the compiled JavaScript to CompiledScript objectSave the compiled JavaScript to CompiledScript object
14.Invoke an function
15.Using thread to run JavaScript by Java
16.Get Script engine by extension name
17.Get a ScriptEngine by MIME type
18.Retrieving Script Engines by Name
19.Retrieving the Metadata of Script Engines
20.Retrieving the Supported File Extensions of a Scripting Engine
21.Retrieving the Supported Mime Types of a Scripting Engine
22.Retrieving the Registered Name of a Scripting Engine
23.Executing Scripts from Java Programs
24.Read and execute a script source file
25.Using Java Objects in JavaScript
26.Pass or retrieve values from a scripting engine (jdk1.6)
27.Use an external file containing the javascript code. The .JS file is loaded from the classpath.
28.Use Java scripting engine
29.Run Javascript function with ScriptEngine
30.Use Java scripting engine (JDK 1.6)
31.Use an external file containing the javascript code
32.Java Language Binding with JavaScript
33.Compresses a String containing JavaScript by removing comments and whitespace.
34.Escape and unescape javascript code
35.Utility to take xml string and convert it to a javascript based tree within html.
36.JavaScript Utils
37.Various Javascript code utilities.
38.JavaScript functions in Java. Internally calls Rhino's implementation of the function calls.