views:

528

answers:

4

I am debugging a Java program in eclipse. This program normally writes binary data to its standard output but can write error messages & stack traces to its standard error stream.

I would like to redirect the binary data to a file, while continuing to show the standard error output in a console. This is trivial when running from the command line. Is it possible under Eclipse?

So far I have only figured out how to redirect both standard output and standard error to the same file. I cannot see a way to separate the two other than adding a new command-line option to the program.


Related: http://stackoverflow.com/questions/799250/i-o-redirection-in-eclipse

A: 

Two solutions come to mind right now for filtering stdout/stderr in an independant console (but they are not for binary streams, hence the Community-Wiki mode):

  • either do not redirect anything (all outputs are in the console), and use a plugin like Grep Console to highlight relevant outputs

alt text

  • and/or redirect everything in a file, and use a plugin like NTail and use some "Filter lines" to exclude selected lines from log file display (that may be closer to what you are looking for).

alt text

(you can define a many a NTail views as you need/want)

VonC
NTail is designed for text filtering and is not suitable for the *binary* data my program normally writes to stdout.
finnw
Right... binary. Oups. I leave this answer in Community-Wiki mode for archive.
VonC
A: 

Well, you can replace System.out with your own PrintStream that writes out to a file. I'm just writing this off the cuff, so maybe it's not exactly perfectly right, but you get the idea:

FileOutputStream fos = new FileOutputStream("c:\myfile.bin");
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintStream ps = new PrintStream(bos);
System.out = ps;

That, or something very similar should do the trick. All output that normally goes to standard out will now go into that file (don't forget to close() the Printstream when you shut your program down, or it won't always flush the last of the data out to the file.)

Of course, you can also do the same trick with System.err if you want.

Xanatos
This works, but I was hoping to find a way to do it within the IDE, without modifying the I/O code of the application
finnw
+2  A: 

Can you try to redirect your stream stdout to a custom View with a SWT Text in it, leaving stderr to the default console.
See this thread

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;


public class ConsumeSysout {


    public static void main(String[] args) {
     Display display = new Display();
     Shell shell = new Shell(display);
     shell.setLayout(new FillLayout());
     final Text text = new Text(shell, SWT.NONE);
     shell.open();


     OutputStream out = new OutputStream() {
      @Override
      public void write(int b) throws IOException {
         text.append(Character.toString((char) b));
      }
     };
     System.setOut(new PrintStream(out));


     System.out.println("Hello World!");


     while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
       display.sleep();
     }
     display.dispose();
    }
}
VonC
+2  A: 

from (http://www.eclipsezone.com/eclipse/forums/t52910.html) If someone would like to see the strings written to System.out/System.err by an already deployed plugin in Eclipse, then it can be done by running eclipse in debug mode from command prompt as eclipse.exe -debug. Or else you can hack a lil bit to capture the stderr/stdout of jvm (running the eclipse) as described in http://www.eclipsezone.com/eclipse/forums/t53216.html

If you run Eclipse in debug mode, you should be able to start it from a command line and include a stderr redirect. In Unix it would be "command 2> file".

This thread has some related content: http://stackoverflow.com/questions/593724/redirect-stderr-stdout-of-a-process-after-its-been-started-using-command-line

This link is more DOS/Windows specific: http://www.teaser.fr/~amajorel/stderr/

The DOS shell, command.com, offers no mean to redirect stderr. Because of that, it's impossible to save to a file, or pipe into a program, the messages that a program prints to stderr. "foo >out" only redirects stdout of foo, not stderr. The latter still goes to the console.

Since stderr is typically where important error messages are printed, this is extremely unfortunate, as this makes us unable to detect errors in unattended jobs.

So what do you do ? One solution is to switch to Unix, where the shell allows one to redirect stderr. With sh and its derivatives, use

foo >out 2>&1

to save both stdout and stderr of foo to out. Similarly, use

foo 2>&1 | bar

to pipe both stdout and stderr of foo into bar. If you're stuck with DOS, Stderr is what you need.

Stderr runs a command and merges its stderr into its stdout. Therefore, redirecting or piping stdout is equivalent to doing the same thing to stderr as well.

I'll have to keep looking for Windows solutions. PowerShell has become popular, it might be worth checking out.

Kelly French
Interesting ideas and links. +1
VonC