Is there any option to clear the console screen in Java as clrscr() in C?
-
4Clear *what* screen? Are you talking about a console application? – Michael Myers Nov 05 '09 at 17:33
-
yes about the console application – user161004 Nov 05 '09 at 17:38
10 Answers
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));

- 398,270
- 210
- 566
- 880
-
3
-
@LB: I admit this is probably the *worst* way to do it. Sometimes you just have to share your WTFs. :) – Bill the Lizard Nov 05 '09 at 18:01
-
3How is this different than `System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");` which is one line instead of five? – James Cronen Nov 05 '09 at 18:03
-
2@Tenner: My code could be written as a function with the char and length passed in. Other than that, no real difference. – Bill the Lizard Nov 05 '09 at 18:06
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")
.

- 3,272
- 7
- 37
- 49
-
2
-
5
-
Trying this option I simply get `error: unreported exception IOexception; must be caught or declared to be thrown.` – ShoeMaker Aug 06 '12 at 16:22
-
`cls` would never work for it is not an inbuilt command. Try using `Runtime.getRuntime().exec("cmd /c cls");` – dumbPotato21 Jan 01 '17 at 17:09
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.

- 5,715
- 2
- 32
- 52
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.

- 53,475
- 11
- 111
- 124
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 ofESC
2J
is for clearing the entire console/terminal screen1;1H
moves the cursor to row 1 and column 1

- 1,930
- 1
- 24
- 30
-
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
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:
The following is a link to a site containing a code as a demonstration of AnsiConsole class contained in the Jansi package:
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.

- 99,456
- 24
- 206
- 195