Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirecting stdout 1

Status
Not open for further replies.

mixer

Programmer
Jan 11, 2000
14
0
0
US
When running an application in a Windows environment (java -c pathstuff apps.MyMain), is there an option to redirect stdout to a text file and not the dos window?

Thanks.
 
Place > followed by the fileName after the java filename, for Example:

java -c pathstuff apps.MyMain > redirectStdOutToTextFile..txt
 
Thanks. I thought this would work also. When I run it locally, it works. When I run it on the network, it creates the file, but does not put any of the Sys.out.println() stuff in it. May have something to do with the network config here. Thanks again.
 
Is there a way to get output from Exceptions redirected as well?
 
I don't know whether you can redirect stderr to a file (via the commandline, without changing your code).
What you can do is use a method like the following :
Code:
  /**
   * Method redirect stdout and stderr to specified directory.
   */
  public static void redirect(File directory) {
    try {
      File tempFile = File.createTempFile("stderr", "", directory);
      System.setErr(new PrintStream(new FileOutputStream(tempFile)));
      tempFile = File.createTempFile("stdout","", directory);
      System.setOut(new PrintStream(new FileOutputStream(tempFile)));
        }
      catch (Throwable t) {
        System.err.println(
          "Error overriding standard output to file.");
        t.printStackTrace(System.err);
      }
    }
You can call this method like (ps : the directory must exist) :
Code:
    redirect(new File("D:\\java\\Temp"));
 
Thanks. I was so focused on the command line I never thought about redirecting System methods in the code. This will work fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top