Java ScriptEngine run Javascript file
import java.io.IOException; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; 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 av a 2s. c o m } try { Path scriptPath = Paths.get("JavaScript.txt").toAbsolutePath(); if (!Files.exists(scriptPath)) { System.out.println(scriptPath + " script file does not exist."); return; } try (Reader reader = Files.newBufferedReader(scriptPath);) { Object result = engine.eval(reader); System.out.println("Result of " + scriptPath + " = " + result); } } catch (ScriptException | IOException e) { e.printStackTrace(); } } }