Java ScriptEngine run Javascript expression
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Main { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); if (engine == null) { System.out.println("JavaScript engine is not available. "); return;/*www . j a va 2s .co m*/ } // Use simple expressions with numeric literals String script = "12.8 + 15.2"; Object result; try { result = engine.eval(script); System.out.println(script + " = " + result); } catch (ScriptException e) { e.printStackTrace(); } } }