Monday, August 10, 2009

Evaluating JavaScript from Java

To evaluate some JavaScript code in java, you have to do the following
  1. Create instance of type ScriptEngineManager
  2. Get JavaScript engine instance by called getEngineByName method
  3. Us e the eval method to evaluate your javascript code
Sample Code

=================================================

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.eval("var x = 5;var y = x + 2");
System.out.println(engine.eval("y"));

=================================================

No comments:

Post a Comment