Can't restore System.out in Groovy (Java)

This is a discussion on Can't restore System.out in Groovy (Java) within the Tech Board forums, part of the Community Boards category; Hi, This is driving me crazy... I'm writing code in Groovy (which runs on Java), and I'm trying to redirect ...

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Can't restore System.out in Groovy (Java)

    Hi,
    This is driving me crazy... I'm writing code in Groovy (which runs on Java), and I'm trying to redirect stdout to a string, run some code, then restore stdout, but after I redirect it, I can't restore it -- it doesn't print to stdout anymore.
    Here's my code:

    Code:
    	// Redirect stdout & stderr to ByteArrayOutputStreams.
    	ByteArrayOutputStream out = new ByteArrayOutputStream();
    	ByteArrayOutputStream err = new ByteArrayOutputStream();
    	PrintStream oldOut = System.out;
    	PrintStream oldErr = System.err;
    	System.setOut( new PrintStream( out ) );
    //	System.setErr( new PrintStream( err ) );
    
    	String stdout;
    	String stderr;
    
    	try {
    		try {
    			app.parseCmdLine( args );
    		}
    		finally {
    			stdout = out.toString();
    			stderr = err.toString();
    	
    			// Restore normal stdout & stderr.
    			System.setOut( oldOut );
    //			System.setErr( oldErr );
    		}
    	}
    	catch ( Throwable e ) {
    		System.setOut( oldOut );
    //		System.setErr( oldErr );
    		System.err.println( "An exception was caught when running:  ${args}" );
    		System.out.println( "*** This should go to stdout." );  // This doesn't print to stdout!
    		throw e;
    	}
    It prints to stderr if I comment out the lines that redirect stderr, but no matter what I do, I can't get it to print to stdout after restoring oldOut. Everything I see online says it should work, so all I can think is that it must be something stupid that Groovy is doing. Any ideas?
    Last edited by cpjust; 05-06-2010 at 10:19 AM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Maybe Groovy is using the SecurityManager?

    I'll be frank; Java is not my forte. But a quick looksie around the Java Platform Docs told me that the only thing keeping you from using System.setOut is a setting in the security manager.
    Consider this post signed

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't think that's the problem.
    The first call to setOut() works fine; it's just the 2nd call where I'm trying to set it back to the original value that I saved in oldOut that doesn't seem to work since I don't see anything displayed to stdout afterwards.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  3. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  4. C/C++ Vs Java
    By Spectrum48k in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-21-2002, 09:06 PM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21