1

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.

orftz
  • 1,138
  • 13
  • 22
Infiniti Fizz
  • 1,726
  • 4
  • 24
  • 40
  • 1
    I have the [bsh-2.0b4.jar](http://www.beanshell.org/bsh-2.0b4.jar) in my classpath and your example works, i.e. the String `out` has the value `test`. – iruediger May 30 '11 at 01:48
  • Hmm what am I doing wrong, I only downloaded bsh-core, could that be my problem? I'll download bsh-2.0b4 and see if that works. Am I correct that setOut() and getOut() returns just the output statements then? – Infiniti Fizz May 30 '11 at 08:10
  • Hmm it's not working for me, my BAOS is empty after the i.eval() statement. Do I need to do anything else, I pretty much only have those lines and bsh-2.0b4.jar in the classpath. – Infiniti Fizz May 30 '11 at 08:22
  • Okay it seems to work if the eval string is "print(\"test\");" because I read the interpreter grabs ITS output but not global System.out outputs so that's why it doesn't work. Did you set anything or change anything to make it grab System.out output? – Infiniti Fizz May 30 '11 at 11:38
  • @Infiniti Fizz: I just created an Eclipse project, added the bsh-2.0b4.jar to the classpath and copied your example. – iruediger May 30 '11 at 11:41
  • Hmm okay thanks for trying it out, I still don't know what my problem is. Are you sure you've grabbed the output in the out String because the eval statement will still print to the console itself as well as (hopefully) be given to String out, so: i.eval("System.out.println(\"test\");"); out = baos.toString(); System.out.println(out); Should print test twice. – Infiniti Fizz May 30 '11 at 12:03
  • 1
    First of all, my apologies for making you waste your time. I've changed one line of your example: `i.setOut(ps);` to `System.setOut(ps);`. I am feeling really stupid, sorry about that. – iruediger May 30 '11 at 12:43
  • Ah no problem, taking a few extra hours to get a solution is much, much better than no solution at all :). My only problem is now I can't print to console can I? Using debug mode I can see that out now equals "test" but then if I try and do System.out.println(out); I get nothing, is that because my PrintStream ps is still grabbing System.out's output? How can I change it back to print to the console? Thanks again for the help iruediger. – Infiniti Fizz May 30 '11 at 15:12
  • Sorry, sorted it myself. For anyone else, do PrintStream ps2 = System.out; then all that code and then reset Systems output stream to ps2 afterwards to print back to console: (After everything) System.setOut(ps2); System.out.println(out); – Infiniti Fizz May 30 '11 at 15:15
  • Also, if you want to write your solution as an answer iruediger I'd be happy to choose it as the correct answer and give you a +1. – Infiniti Fizz May 30 '11 at 15:15
  • 1
    No need to. I am glad I could help. – iruediger May 30 '11 at 17:18

1 Answers1

0
Interpreter i = new Interpreter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);

System.setOut(ps);

try {
  //i.eval("System.out.println(\"test\");");
  i.source("c:\\htdocs\\test.bsh");
} catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

String out = "hello : "+baos.toString();
System.err.println(out);
j.w.r
  • 4,136
  • 2
  • 27
  • 29
doomy
  • 1