Displaying the console in your RCP application
This question has been asked on eclipse.newcomer a couple of times now, so I thought it might be worthwhile to put a response in the blogosphere.
How do I display the System.out console in a view in my Eclipse RCP application?
The answer has two parts. To start, you can redirect console output to an arbitrary PrintStream using System.setOut(somePrintStream);. The second part is that you need a useful PrintStream to write on.
It’s relatively easy to make a plug-in that does exactly what we need. Here’s how you construct the view.
package org.eclipse.console.ui.views;
import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;
import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.part.ViewPart;
public class LightweightConsoleView extends ViewPart { private Text text;
public void createPartControl(Composite parent) { text = new Text(parent, SWT.READ_ONLY | SWT.MULTI); OutputStream out = new OutputStream() { @Override public void write(int b) throws IOException { if (text.isDisposed()) return; text.append(String.valueOf((char) b)); } }; final PrintStream oldOut = System.out; System.setOut(new PrintStream(out)); text.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { System.setOut(oldOut); } }); }
public void setFocus() { text.setFocus(); }}
You’ll notice that I’ve created a read-only Text to contain the output. Then I create an anonymous OutputStream that–when told to dump some bytes–appends output to the end of the Text; I wrap that OutputStream in a PrintStream, and we’re off to the races.
The one part that I’m not so comfortable with is making things right again after the console view closes. In this implementation, I remember where output used to be directed and reset it after the view closes. As long as this view is the only one that tries to mess with the console, this will work. As soon as another view tries, things will get a little spooky.
Posted October 19th, 2006 by Wayne Beaton in category: Other
You can skip to the end and leave a response. Pinging is currently not allowed.
26 Responses to “Displaying the console in your RCP application”
Leave a Reply
You must be logged in using your Eclipse Bugzilla account to post a comment.


Jeroen Leenarts Says:
October 19th, 2006 at 11:14 am
A logical follow-up would be this one: Any ideas on how to implement a “power-user console”?
Sort of to satisfy any text based itches one might have? I can imagine that it can be usefull to be able to edit/query an application’s model through a mini language.
Should be easy to implement the laguage side with any java based scripting language. (The name of one of those is sitting at the tip of my tongue, can’t remember…
)
Anonymous Says:
October 19th, 2006 at 12:25 pm
Converting every byte in the stream to a string and then appending the string to a text field (which is going to cause a repaint) has got to be slow…
Dominique Boucher Says:
October 19th, 2006 at 1:40 pm
Wayne,
I translated your Java code in Scheme to show how one can script Eclipse using the SchemeScript plugin.
Anonymous Says:
October 20th, 2006 at 6:56 am
Not bad. But I think that would be more interested in seeying how to integrate the *eclipse console view* in your RCP application than seeying how to reimplement the wheel.
Anonymous Says:
November 9th, 2006 at 2:00 pm
Hello,
I’m using MessageConsole (RCP) and ConsoleManager.
There is a bar that you can open new consoles and I want to DO NOT open new consoles.
Anyone have ideas?
Thanks
spocket Says:
January 29th, 2007 at 11:55 am
My version of the write-method. This one can also handle calls from threads other than the UI-Thread.
@Override
public void write(int b) throws IOException
{
final String toAppend = String.valueOf( (char) b );
display.asyncExec( new Runnable()
{
public void run()
{
if (text.isDisposed())
return;
text.append( toAppend );
}
} );
}
Wayne Says:
January 29th, 2007 at 6:11 pm
Very nice. That should also handle multiple concurrent threads attempting to write to System.out.
Anonymous Says:
February 2nd, 2007 at 7:53 pm
I needed to do this in my RCP application, but I wanted the features/feel of the Eclipse console… So, in my Application.java run() I have the following before creating and running the workbench:
MessageConsole console = new MessageConsole(”System Output”, null);
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
MessageConsoleStream stream = console.newMessageStream();
System.setOut(new PrintStream(stream));
System.setErr(new PrintStream(stream));
logger = LoggerFactory.getLogger(Application.class); // Previously declared.
This makes System.out, System.err, and anything that any subclass in my RCP app logs using logback magically appear in the standard Eclipse console view. I haven’t tested performance, but it seems fine so far. Feedback is welcome!
Sebastien Says:
February 21st, 2007 at 2:44 pm
Hello!
I am currently developing a console that will recieve logging events (for logback, too).
So far, I’ve been able to send content to a MessageConsole, and add it to the ConsolePlugin tab.
What I would like to do is 3 things:
1. Integrate the MessageConsole in my view, and not add it to the ConsoleView.
2. Color some logging statements
3. Remove old lines, so that my view does not eat all the memory available if 10000 events are recieved.
Could anybody give me some hints on how to procede? The first of the previous points is what I’m on at the moment and I am not that close to having a nice result…
Thanks!
acorona Says:
April 11th, 2007 at 8:41 pm
Very useful tip. But I found it kind of slow. So I changed the implementation of the OutputStream to buffer the output causing a quicker display of the output. Here is my version of the OutputStream:
OutputStream out = new OutputStream() {
StringBuffer buffer = new StringBuffer();
@Override
public void write(final int b) throws IOException {
if (text.isDisposed())
return;
buffer.append((char) b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
flush();
}
@Override
public void flush() throws IOException {
final String newText = buffer.toString();
top.getDisplay().syncExec(new Runnable() {
public void run() {
text.append(newText);
}
});
buffer = new StringBuffer();
}
};
acorona Says:
April 17th, 2007 at 8:39 pm
The OutputStream I posted before has a problem with multiple threads. I’ve since changed the code and this seems to work better.
—–
OutputStream out = new OutputStream() {
private StringBuffer buffer = new StringBuffer();
private final Object obj = new Object();
@Override
public void write(final int b) throws IOException {
synchronized (obj) {
if (text.isDisposed())
return;
buffer.append((char) b);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
flush();
}
@Override
public void flush() throws IOException {
synchronized (obj) {
final String newText = buffer.toString();
text.append(newText);
buffer = new StringBuffer();
}
}
};
Albert G Says:
July 19th, 2007 at 10:48 am
Bookmarks…
How I add this article to Digg?…
MOBY Says:
August 9th, 2007 at 3:38 pm
Bookmarks…
I can’t add your post to Digg. How I do this?…
A blue Says:
August 14th, 2007 at 7:50 am
A blue…
NetscapeBot: Add your article to Netscape.com…
America the Beautiful Flag Silver Charm Says:
August 14th, 2007 at 7:52 am
America the Beautiful Flag Silver Charm…
NetscapeBot: Add your article to Netscape.com…
Mr.Rick Says:
August 15th, 2007 at 1:57 pm
Add Page…
I don’t added your post to del.icio.us…
Lavender Awareness Ribbon Photo Charm Says:
August 16th, 2007 at 6:25 am
Lavender Awareness Ribbon Photo Charm…
I don’t see admin e-mail…
Kenneth Killin Says:
August 17th, 2007 at 2:33 am
British Columbia Aerial Photography, Vancouver, Photo, Aerial Video - Flightpath Film, Video and Stills - Flightnet…
Useful, thank you!…
roll control bolsters Says:
August 17th, 2007 at 8:56 am
roll control bolsters…
I don’t see Subscribe to this blog…
design padding specialized Says:
August 17th, 2007 at 9:13 am
design padding specialized…
I don’t see Subscribe to this blog…
Mr.Roy Says:
August 17th, 2007 at 11:00 am
Add Article…
How I add article to your blog?…
Isaac Baber Says:
August 18th, 2007 at 3:30 pm
Hotels and Travel Services in Turkey…
Thank you for your post!…
joel bolster Says:
August 21st, 2007 at 2:26 am
joel bolster…
Where I see gallery?…
pillow talk meaning Says:
August 21st, 2007 at 2:42 am
pillow talk meaning…
Where I see gallery?…
que bebo se que fumo Says:
August 21st, 2007 at 8:50 am
que bebo se que fumo…
Wellcome to Bebo online ?ommunity, allowing you to share photos and blogs…
register for bebo Says:
August 21st, 2007 at 9:09 am
register for bebo…
Wellcome to Bebo online ?ommunity, allowing you to share photos and blogs…