Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear screen option in java [duplicate]

Tags:

java

Is there any option to clear the screen in java as clrscr() in C.

like image 857
user161004 Avatar asked Nov 05 '09 17:11

user161004


2 Answers

For any console which supports ANSI escapes the following would work (would e.g. work in Win98 console).

private final String ANSI_CLS = "\u001b[2J";
....
System.out.print(ANSI_CLS);
System.out.flush();
...

Starting with Win NT this won't work anymore and you can either

  • Do a JNI call (e.g. like here: Java: Clear console and control attributes
  • Or write out a bunch of empty lines

Otherwise you are out of luck.

And btw. you must keep in mind that System.out and System.err don't have to be console they could be set to what ever (writing into a file e.g.) an usecase where clearing the screen wouldn't make any sense at all.

like image 175
jitter Avatar answered Oct 16 '22 23:10

jitter


You can also try ANSI Escape Codes:

If your terminal support them, try something like this:

System.out.print("\033[2J\033[1;1H");

You can include \0333[1;1H to be sure if \0333[2J does not move the cursor in the upper left corner.

More specifically:

  • 033 is the octal of ESC
  • 2J is for clearing the entire console/terminal screen
  • 1;1H moves the cursor to row 1 and column 1
like image 33
gon1332 Avatar answered Oct 17 '22 00:10

gon1332