162

How can we redirect the eclipse console output to a file? I can:

  1. Run Configuration->Commons->Select a file.
  2. Use System.setOut(PrintStream) and System.setErr(PrintStream).

The problem with 1) is that I need to record console output to different files instead of one file.

The problem with 2) is that it only stores console output generated by System.out.println() or stacktrace of an exception thrown. It does not capture other kind of outputs e.g. log4j warnings etc.

How can we redirect console output to different files programmatically?

Stephan
  • 41,764
  • 65
  • 238
  • 329
Ali Shah
  • 1,621
  • 2
  • 11
  • 3
  • 1) Maybe you shoude be use "Variables.." (ex) C:\test${???:??}.txt – cloverink Apr 19 '11 at 09:22
  • 2
    Here's an idea. Write your own eclipse plugin (ie., a cusotm button) which would assign a random but unique value to a variable (called `${NAME}`), then would do the same thing as run. Then use `$NAME` as the value for the output file. maybe? – One Two Three Jan 28 '15 at 03:10

5 Answers5

176

Go to run as and choose Run Configurations -> Common and in the Standard Input and Output you can choose a File also.

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
  • 107
    I find it absolutely hilarious that 40 people upvoted a copy-paste from the second line of the question. – AnthonyW Jul 24 '13 at 16:39
  • 14
    @AnthonyW I think that's mostly because people are led here by the title question which is "How can we redirect eclipse output to a file". This answer is in fact an answer to the title question. – Gerome Bochmann Mar 20 '15 at 14:50
  • 1
    If you can't find "Run Configurations", using Eclipse 4.4 (Luna), I opened my Launch Configuration (by double clicking on the server in the Servers tab), clicked on "Open lanuch configuration", then went to the Common tab. In the bottom half of that dialog, there is a section called "Standard Input and Output". Here, I could click on File and give it a file name. – Guy Schalnat Jan 05 '16 at 17:58
  • 1
    Hint: open a terminal, `echo $TTY` and specify whatever it returns as your file. Then the terminal will mirror what is in your Eclipse console. – Sridhar Sarnobat Jan 28 '16 at 21:48
  • @LuísdeSousa Then why don't you edit the answer and add how you can do it on Luna as well – Shervin Asgari Apr 26 '16 at 12:41
  • 2
    What? This answer is incorrect when reading the questioner's question carefully. – ComputerScientist Jul 11 '16 at 21:05
11

You could use a "variable" inside the output filename, for example:

/tmp/FetchBlock-${current_date}.txt

current_date:

Returns the current system time formatted as yyyyMMdd_HHmm. An optional argument can be used to provide alternative formatting. The argument must be valid pattern for java.util.SimpleDateFormat.

Or you can also use a system_property or an env_var to specify something dynamic (either one needs to be specified as arguments)

mike32b
  • 426
  • 4
  • 6
8

You can set the output of System.out programmatically by doing:

System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("/location/to/console.out")), true));

Edit:

Due to the fact that this solution is based on a PrintStream, we can enable autoFlush, but according to the docs:

autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written

So if a new line isn't written, remember to System.out.flush() manually.

(Thanks Robert Tupelo-Schneck)

Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
1

To solve the problem I use ${string_prompt} variable. It shows a input dialog when application runs. I can set the date/time manually at that dialog.

  1. Move cursor at the end of file path. enter image description here

  2. Click variables and select string_prompt enter image description here

  3. Select Apply and Run enter image description here enter image description here

iOS-Developer84
  • 654
  • 8
  • 19
0

We can do this by setting out variable of System class in the following way

System.setOut(new PrintStream(new FileOutputStream("Path to output file"))). Also You need to close or flush 'out'(System.out.close() or System.out.flush()) variable so that you don't end up missing some output.

Source : http://xmodulo.com/how-to-save-console-output-to-file-in-eclipse.html

drp
  • 340
  • 1
  • 13