title: How to capture output from Beanshell
body: <p>I've been using BeanShell to interpret simple files that just do some calculations and then output to the console. Thing is, I want to grab the output. Such that from <code>System.out.println("test");</code> I can get <code>"test"</code> as a string to put somewhere else.</p>
  
  <p>I've looked at <code>Interpreter.getOut()</code>, but I haven't managed to understand what it's actually for (the documentation isn't that useful). I tried grabbing the <code>PrintStream</code> using <code>getOut()</code> and then printing its contents, but it is empty. I also tried the following after messing around:</p>
  
  <pre><code>Interpreter i = new Interpreter();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  PrintStream ps = new PrintStream(baos);
  i.setOut(ps);
  i.eval("System.out.println(\"test\");");
  String out = baos.toString();
  </code></pre>
  
  <p>But that is also empty.</p>