Intercept script output

suggest change
// Obtain an instance of JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

// Setup a custom writer
StringWriter stringWriter = new StringWriter();
// Modify the engine context so that the custom writer is now the default
// output writer of the engine

// Execute some script
try {
    engine.eval("print('Redirected text!');");
} catch (ScriptException ex) {

// Outcome:
// Nothing printed on standard output, but
// stringWriter.toString() contains 'Redirected text!'

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents