By Alvin Alexander. Last updated: June 6, 2016
I just learned how to send STDOUT and STDERR to a file in a Java application (without using Unix shell redirect symbols). Just add these two lines of code to the main
method in your Java (or Scala) program:
System.setOut(new PrintStream("/Users/al/Projects/Sarah2/std.out")); System.setErr(new PrintStream("/Users/al/Projects/Sarah2/std.err"));
Then when you use:
System.out.println("foo");
or
System.err.println("bar");
the output from those println
methods will be written to those files.