title: Java 8 Nashorn - capturing engine.eval("print('hello world')) into String object?
body: <p>Is there a way to capture the result of print('hello world') inside Nashorn and place it in a String object.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();
  PrintStream ps = new PrintStream(baos, true);
  System.setOut(ps);
  
  String result = (String)engine.eval("print('hello world')");
  
  if (result != null) {
      System.out.println(result);
  } else {
      System.out.println(baos.toString());
  }
  </code></pre>
  
  <p>When engine evaluates this javascript it just prints to stdout so I figured I just redirect stdout to my own OutputStream and then convert it to String, but it doesn't work.</p>
  
  <p>Any thoughts? </p>