3

I created a sample Java application. I want to clear the window options, i.e.:

  1. Register
  2. Login
  3. Clear

If the user presses 3 I need to programmatically clear all options. Something like Console.clear?

Is there any way that I can do this with Java?

braX
  • 11,506
  • 5
  • 20
  • 33
  • 3
    Possible duplicate of: http://stackoverflow.com/questions/2979383/java-clear-the-console – Baz Aug 05 '12 at 18:39
  • It’s impossible, you’ll have to clear the lines. See at this old question: [How do you clear the Java Console?](http://stackoverflow.com/questions/606086) – Michael Piefel Aug 05 '12 at 20:18

4 Answers4

5

You will need to output a bunch of blank lines. Even in Windows/*nix, clear/cls doesn't truly clear the screen, it just prints enough blank lines that you cannot see the previous text.

Kevin Mangold
  • 1,167
  • 8
  • 21
  • This is not correct. The Windows `cmd` command `cls` does clear the console, including the history, i.e. you can not scroll up to see the previous text again. Afaik, the clear command does not remove the history, still, it won’t cause having a bunch of blank lines above the cursor position. – Holger Nov 14 '19 at 08:43
  • @Holger At the time of this answer's posting, cls did not behave the same way it does now. – Kevin Mangold Nov 14 '19 at 19:09
1

You can try System.out.print("CLS");

Or use loops to clear the screen like

public static void clearScreen() {  
System.out.print("\033[H\033[2J");  
System.out.flush();  
}  

and then call this method clearScreen(); if you want to clear.

Sorry my english is bad. :) I just want to help you.

Nam
  • 11
  • 2
0

If you are working with the console, then these might prove useful. Using "backspace" character. Using process builder.

Community
  • 1
  • 1
SpirosMesa
  • 69
  • 1
  • 1
  • 10
-1

You mean you created a console application and want to clear the console (not necessarily the Eclipse console)?

If so then I guess you're looking for:

Runtime.getRuntime().exec("cls");

But be aware this will be system dependent.

Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94