Java clearing the consoleCreated by maxtingle on Sat 9th May 2015 in category Programming. 1147 Views | Tags: javaconsoleclearannoyingrantMy oh my, what fun I've had with Java recently.
For one of my upcoming projects (Yes I actually have been working on stuff, just a bit busy getting used to 9 hour work days right now) I have been working in Java due to the fact that C# is only cross platform for web applications and this is a desktop application.
Part of this project is a server and what does a server need to do? Run from the command line. This means that the entire server application and all its output go through the console, more specifically System.out.print(ln).
Now, given that it's a server, there are certain stats one might one to track, like the flow of connections. This meant the creation of a persistent messaging system, not that hard to do, just have an output buffer and update the buffer with the new persistent message content, at-least that's what I thought.
Before I start, let me give you a quick overview of some languages I've written in the past and how they handle console clearing:
C#
Console.Clear()
JavaScript
window.console.clear()
PHP
ncurses_clear(); OR system("cls")
Now lets look at Java, first off you'd look for a System.out.clear method, not there, that's okay lets just check the buffered writers that can wrap around System.out, maybe BufferedWriter will help us? Nope. BufferedWriter doesn't actually clear the console.
Using system commands
Fair enough, how about system commands? Lets try it out, after all the Runtime class lets us access the console directly..
Runtime.getRuntime().exec(System.getProperty("os.name").toLowerCase().contains("windows") ? "cls" : "clear");
Failed to start process cls
Wat... But it's running from within the console! Nope, it's running through the JVM process and the JVM process doesn't have cls or clear as a command, so it tries to execute cls as an application.
Using system commands directly
Okay lets access cmd.exe directly, it may only work on windows but we can add support for other OS' later. How about these apples!
Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "cls"});
UHUR - That spawns a new cmd.exe process, at this point you might be getting a little miffed, that's fine, I was too.
Using backline
When you output \r in Java, it gets rid of the last word or line if your pointer is at the start of it. Sweet! Jackpot! Boooooya! Surely now we can just loop over all the words or characters in the output buffer and output \r for each one, thereby deleting all the previously outputted words.
for(int i = 0; i < _buffer.split(" ").length; i++) {
System.out.print("\r");
}
UHUR - For some reason, \r will only delete a maximum of a single word...
Using backspace
Alright \r doesn't work, lets get more creative, lets try \b. That's right, we'll print out the backspace character for every character in the output buffer, thereby deleting all characters we've printed!
for(int i = 0; i < _buffer.length(); i++) {
System.out.print("\b");
}
UHUR - It doesn't actually delete any characters.. Not much use at all then.
The final solution - Printing lines
And so having being beaten on many accounts, I am left with but one option, to print out 11 lines to the console which will result in the illusion of clearing the console. I'm not going to bother tainting your eyes with the disgusting code, but it should be fairly obvious how it works. Why 11 lines you might ask? Because I counted the number of lines in the default size terminal and that's how many there is, no you cannot get the size of the console because despite writing to it and reading from it, Java does not actually know the console exists, it's just some streams to Java.
I will solve you one day newline issue, but for now, this will have to do..