48

I am running various Java benchmarks and would like to archive the results. I execute the (dacapo) benchmark like this:

C:\VM\jre\bin\java  -jar C:\benchmarks\dacapo-9.12-bach.jar %arg1% > %time::=%

I pass the type of benchmark in over a parameter, thats what %arg1% is.

You can see that I am redirecting the output to a textfile. Unfortunately, the first and last line of the output is still printed in the console and not into the textfile:

===== DaCapo 9.12 luindex starting =====
===== DaCapo 9.12 luindex PASSED in 2000 msec =====

Especially the last line would be important to have in the text file :)

Is there a trick to force this behavior?

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Nicolas
  • 1,828
  • 6
  • 23
  • 34

2 Answers2

87

You must redirect STDOUT and STDERR.

command > logfile 2>&1

STDIN is file descriptor #0, STDOUT is file descriptor #1 and STDERR is file descriptor #2.
Just as "command > file" redirects STDOUT to a file, you may also redirect arbitrary file descriptors to each other. The >& operator redirects between file descriptors. So, 2 >& 1 redirects all STDERR output to STDOUT.

Furthermore, take care to add 2>&1 at the end of the instruction because on Windows, the order of redirection is important as command 2>&1 > logfile will produce an empty file, as Dawid added in the comments.

Immoskyl
  • 53
  • 10
Zach Riggle
  • 2,975
  • 19
  • 26
  • 3
    What does the 2>&1 even mean? To say it's unintuitive syntax would be a compliment of the highest order. – vargonian Nov 18 '15 at 01:22
  • 13
    STDIN is file descriptor #0. STDOUT is file descriptor #1. STDERR is file descriptor #2. Just as "command > file" redirects STDOUT to a file, you may also redirect arbitrary file descriptors to eachother. The ">&" operator redirects between file descriptors. So, "2 >& 1" redirects all STDERR output to STDOUT. – Zach Riggle Dec 16 '15 at 05:42
  • Thank you so much, Zach, this demystifies it! – vargonian Dec 29 '15 at 14:50
  • @ZachRiggle upvoted, but could you please add your comment about STDIN/STDERR and the explanation to the answer? I think this is very helpful info! – aikeru Aug 16 '17 at 13:12
  • on windows order of redirections is important as `command 2>&1 > logfile` will produce empty file – Dawid Feb 08 '18 at 11:04
8

Add 2>&1 to your command:

 C:\VM\jre\bin\java  -jar C:\benchmarks\dacapo-9.12-bach.jar %arg1% 2>&1 > %time::=% 

This will redirect STDERR to STDOUT which is then redirected into your textfile.

Squeezy
  • 495
  • 4
  • 11