PDA

View Full Version : Clearing the console in Java


dcoder
09-15-2005, 04:31 PM
I'm going mad everyone.

All I want to do is clear the console's buffer (i.e. "cls in Windows, "clear" in Unix).

I've googled this quite a bit and found nothing useful.

Sun's forums are completely useless, there are about 20 threads asking the same question and the only replies are to put in a loop with a bunch of System.out.println(). There is another "solution" but it's a nasty hack that involes editing system files (and thus makes it difficult to distribute my application!).

I'm just not convinced that there's "no way to do it."

Does ANYONE know how to clear the console??!

Thanks,
Kyle

Edit: Also, I've tried using Runtime.exec() to execute the "cls" command, but that seems to start a new console session.

nikkiH
09-16-2005, 04:43 PM
There is a way -- by outputting 80 empty lines. :D

Java does not assume standard out is a console. Therefore, it has no way to clear it. Java is about cross-platform applications.
If you want to clear the console, you have to be making an assumption there IS one. Therefore, it is no longer cross-platform.

dcoder
09-16-2005, 05:37 PM
Heh. Again, I refuse to print a bunch of empty lines. There's got to be a more elegant way to do it!

And just because Java doesn't assume standard out is a console doesn't mean there isn't a way to clear it. Java can write to the console without having to assume standard out is a console, so why should it have to assume this in order to clear it?

Maybe I'm just being to stubborn with this one :D ...I may have to email Sun.

dcoder
09-16-2005, 05:44 PM
Alright, I admit defeat. I just followed the heirarchy upwards from the PrintStream 'out' in System, and it looks like nowhere in this heirarchy is a formal method for clearing the OutputStream defined.

:(

I guess I'll have to find another way to write text that can easily be cleared...

nikkiH
09-16-2005, 06:53 PM
Well, why are you using the console for this, then?
Use a real GUI (swing, etc) and you can clear whatever you want...