Redirecting output using PrintStream object?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Redirecting output using PrintStream object?

Stuart White
I'm calling beanshell from a Java app and am trying to redirect the
output to a file.  I can't seem to figure out how to make this work.

Here's my code:

----------------------------------------------------------------------------

import bsh.Interpreter;
import java.io.*;

public class wsbuilder {

   public static void main(String[] args) {

      Interpreter bsh = new Interpreter();

      try {
         PrintStream out = new PrintStream(new FileOutputStream("out.txt"));
         bsh.setOut(out);
         bsh.eval("System.out.println(\"Hello world\");");
      } catch (Exception e) {
         System.err.println(e.toString());
         return;
      }
   }
}

----------------------------------------------------------------------------

When I run this, "Hello World" gets printed to the console, and
nothing gets written to the file "out.txt".  Would someone please show
me the error of my ways?  Thanks!


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Redirecting output using PrintStream object?

Shankar Unni
Stuart White wrote:

>          PrintStream out = new PrintStream(new FileOutputStream("out.txt"));
>          bsh.setOut(out);

AFAICT this only affects the output of the "print()" builtin (via
Interpreter.print() and Interpreter.println()).

Bsh doesn't trap the System's "out", because that's global to the entire
VM. You'd have to redirect it yourself by doing "System.setOut()"
(outside BeanShell), but that would trap System.out.println() from
*everywhere* (inside your interpreter as well as anywhere else in your
program).

(I wonder if it would be possible to create a fake "System" object
inside bsh that would act as a proxy for all the System APIs, but do
"the right thing" for System.out and System.err, returning
Interpreter.out and Interpreter.err).



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Redirecting output using PrintStream object?

Jacek-8
In reply to this post by Stuart White
I have the same problem. Did you manage to solve this somehow?
/Jacek





-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users