4

I have been on this for a while now, and for the past three days have ripped apart the Internet for ways to effectively clear the console in Java.

Ways I have seen it "done"

This way for(int x = 0; x!=100; x++){ System.out.println(); } Sucks, as you can just scroll up and see the printed statements again.

Console.Clear(); and all variations of it, have not worked for me.

Runtime.getRuntime().exec("cls"); has not worked in any cases i have tried to use it in.

(I use JCreator to code, I have no idea if this has anything to do with my issue)

This way by joesumbody122, looked interesting:

 private static void clearLine()
 {
       Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));
 }

and

private static void clearLine(int left, int top)
{    

int pLeft = Console.CursorLeft;
int pTop  = Console.CursorTop;

Console.setCursorPosition(left, top);
Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));

Console.setCursorPosition(pLeft, pTop);
}

But sadly i could not get it to work for me. It gave me errors that all the methods that he called from Console did not exist. java.io.*; was imported His method clears one line specifically, so if someone could get this working, (Again, I use JCreator to code, I have no idea if this has anything to do with my issue) I could see it being looped to clear all the lines.

Ways to make less sucky?

Back to this for(int x = 0; x!=100; x++){ System.out.println(); } Is there a way to prevent the user from scrolling up in the command line? To set the cursor to the top left of the prompt? That would make this method a whole lot more useful.

Another Theory

Is there a way to simply tell java to stop printing in one command line, start printing in another, and close the window of the first so it only appears to have cleared the console, but instead created an entirely new one? I have pondered this the last two hours, and in theory it would work, but i don't know if the code to do so even exists.

braX
  • 11,506
  • 5
  • 20
  • 33
REGAL
  • 326
  • 2
  • 9
  • There is always the option of creating (a simple) console with swing for output/input of your program. – arynaq Oct 11 '13 at 17:00
  • @arynaq Yes, i know, but i wish to leave it in the console, i like the way it looks. – REGAL Oct 11 '13 at 17:02
  • Are you talking about a command-line Java app? What platform (Linux, Windows, ...)? – ct_ Oct 11 '13 at 17:03
  • Check this question out: http://stackoverflow.com/questions/4799006/in-java-how-can-i-redirect-system-out-to-null-then-back-to-stdout-again – Marcelo Oct 11 '13 at 17:07
  • @ct_ Yes, a Basic Java Application in JCreator on Windows 7 – REGAL Oct 16 '13 at 13:42

3 Answers3

4

There is no reliable way that works everywhere. You've already mostly discovered this.

Generally, command-line output goes into a terminal scrollback buffer, of which only the last n lines are displayed, but previous lines are available through a scrolling mechanism. A command-line program does not write directly to this buffer, but writes to stdout which is, in most cases, piped to the terminal process which then displays the data.

Some terminal programs (i.e. those supporting ANSI escapes) might let you clear the visible portion of the screen. As far as I know, only Windows' cmd.exe responds to a 'clear screen' request by clearing the entire scrollback buffer. On Linux AFAIK it's not possible to discard the buffer completely. And, on Windows, cls is not an executable command but a shell builtin, so you cannot run it from Java System.exec().

Also, any command-line program can have its output redirected to a file with out the program being aware of it, in which case 'clear screen' doesn't have much meaning.

If you MUST have this level of control then you will have to write your own display window using Swing, AWT or GWT or whatever and manage all interaction there.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Could you possibly give me an example of his working so that i could edit it and insert my own text, inputs, variables, and the like? – REGAL Oct 14 '13 at 16:42
  • 1
    Sorry, that's beyond the scope of SO. You'll need to learn enough UI programming to do it yourself. – Jim Garrison Oct 14 '13 at 17:49
1

If it's a command-line app and the terminal/shell you're running the app in supports ANSI escape codes then you can just do this:

System.out.print("\033[H\033[2J");
System.out.flush();
ct_
  • 2,269
  • 1
  • 16
  • 28
1

The answer depends on weather or not you are using Linux or Windows OS. If you are using linux and want to clear the console then try:
try {
new ProcessBuilder("/usr/bin/clear").inheritIO().start().waitFor();
} catch(Exception e) {}

If you are using windows try:
try {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} catch(Exception e) {}

You can handle the exception any way you want.It does not matter becasue you will not get one.