Java tutorial
import java.io.File; import java.io.FileWriter; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Main { public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); File outputFile = new File("jsoutput.txt"); System.out.println("Script output will be written to " + outputFile.getAbsolutePath()); FileWriter writer = new FileWriter(outputFile); ScriptContext defaultCtx = engine.getContext(); defaultCtx.setWriter(writer); String script = "print('Hello custom output writer')"; engine.eval(script); writer.close(); } }