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?

2
  • 1) Maybe you shoude be use "Variables.." (ex) C:\test${???:??}.txt Commented Apr 19, 2011 at 9: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? Commented Jan 28, 2015 at 3:10

5 Answers 5

176

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

Sign up to request clarification or add additional context in comments.

6 Comments

I find it absolutely hilarious that 40 people upvoted a copy-paste from the second line of the question.
@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.
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.
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.
What? This answer is incorrect when reading the questioner's question carefully.
|
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)

Comments

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)

1 Comment

It seems that you also need to close with System.out.flush() in order to avoid possibly missing some output.
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.