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 System.out.println("test");
I can get "test"
as a string to put somewhere else.
I've looked at Interpreter.getOut()
, but I haven't managed to understand what it's actually for (the documentation isn't that useful). I tried grabbing the PrintStream
using getOut()
and then printing its contents, but it is empty. I also tried the following after messing around:
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();
But that is also empty.