10

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

user161004
  • 967
  • 3
  • 14
  • 23

10 Answers10

12

As dirty hacks go, I like msparer's solution. An even dirtier method that I've seen used (I would never do this myself. I swear. Really.) is to write a bunch of newlines to the console. This doesn't clear the screen at all, but creates the illusion of a clear screen to the user.

char c = '\n';
int length = 25;
char[] chars = new char[length];
Arrays.fill(chars, c);
System.out.print(String.valueOf(chars));
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
8

If you're talking about a console application, then there isn't a clear screen option AFAIK. A quite dirty option would be to invoke the clear screen command of the underlying OS.

Then it's something like

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

for Windows or

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

for a load of other OS. You can find out the OS with System.getProperty("os.name").

msp
  • 3,078
  • 7
  • 34
  • 47
7

If you're talking about the console, then no. Writing to the console is just a special case of an output stream. Output streams don't know anything about the screen, as they can be just as easily redirected to a file or another system device.

James Cronen
  • 5,545
  • 2
  • 29
  • 48
5

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

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.

jitter
  • 51,939
  • 11
  • 106
  • 120
4

On linux, you can do something like:

System.out.println("\f");

You can also use Jcurses

LB40
  • 11,121
  • 16
  • 66
  • 104
3

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
gon1332
  • 1,630
  • 1
  • 20
  • 27
  • This is the only ANSI-solution which consistently works inside my Linux terminal. Especially the cursor repositioning at the end makes it look nice and clean. Moreover, it respects the size (height) of the window. – Doe Johnson Jul 10 '15 at 07:23
3

To clear the screen just type:

System.out.print('\u000C');
sth
  • 200,334
  • 49
  • 262
  • 354
Dhickey90
  • 31
  • 1
2

Jansi is an excellent workaround. I am an amateur coder and Jansi is easy to setup especially with Eclipse.

The following is a link to the homepage of Jansi:

http://jansi.fusesource.org/

The following is a link to a site containing a code as a demonstration of AnsiConsole class contained in the Jansi package:

http://www.rgagnon.com/javadetails/java-0047.html

0

For Windows, Java Console API project provides functionality to determine console size and set cursor position. Clearing the screen is trivial with that. It's a version 0.2 now so it's not exactly production ready, but it works.

Alternatively, you can simply print out some new lines via System.out.println(). 640 should be enough for everybody :-) It's not the same as clearing screen, but for user's intents and purposes it'd do.

ChssPly76
  • 94,877
  • 24
  • 194
  • 191
0

you should give a try with JNA and try mapping native libraries:

  • on linux you must map C functions from ncurses library
  • on windows you must map functions from both msvcrt and kernel32, as clearly stated here

PS

let me known if you need some sample code

dfa
  • 107,531
  • 29
  • 184
  • 223