What command in Java will let you clear the console in a command-line application?
11 Answers
I think what the OP wants is to clear the screen and move the cursor to the home position. For that try:
final String ANSI_CLS = "\u001b[2J";
final String ANSI_HOME = "\u001b[H";
System.out.print(ANSI_CLS + ANSI_HOME);
System.out.flush();

- 419
- 4
- 8
It depends on your console but if it supports ANSI escape sequences, then try this..
final static String ESC = "\033[";
System.out.print(ESC + "2J");

- 52,720
- 19
- 113
- 137
-
1Something like this is probably your best bet, as I don't think there's a real Java solution (+1) – Sean Patrick Floyd Feb 03 '11 at 16:01
-
-
@wardedmocha: this would only work on an ANSI terminal emulator. It won't work on a vanilla Windows command prompt. DOS used to have ANSI.SYS, but on modern Windows systems it only works for DOS (I mean actual 20-year old DOS, not just CLI) applications. – thkala Feb 03 '11 at 16:16
clearing for bash that is working for me:
System.out.print(String.format("\033[H\033[2J"));

- 31
- 1
Run this sample program: it demonstrates how to clear the console using an escape sequence and reposition the cursor to position X=1, Y=1.
I tested it on several Linux terminals. Don't know, if it works under Windows.
Perhaps you can tell me ;)
Read this article about escape sequences.
import java.io.*;
public class Main {
public static final char ESC = 27;
public static void main(String[] args)
throws Exception {
Console c = System.console();
if (c == null) {
System.err.println("no console");
System.exit(1);
}
// clear screen only the first time
c.writer().print(ESC + "[2J");
c.flush();
Thread.sleep(200);
for (int i = 0; i < 100; ++i) {
// reposition the cursor to 1|1
c.writer().print(ESC + "[1;1H");
c.flush();
c.writer().println("hello " + i);
c.flush();
Thread.sleep(200);
}
}
}

- 10,748
- 12
- 69
- 103
-
I liked the script. I would to know if is there a way to put the text of the console into a variable before clearing the console, and later restore the console back to the state it was before ? Probably doing something like `buffer = System.out; ` `System.out.print(ESC+"[1;1H"); ` `System.out.flush(); ` `System.out.println("Lorem ispum ....");` `System.out.print(ESC+"[1;1H"); ` `System.out.flush(); ` `System.out.println(buffer);` – maan81 Aug 16 '12 at 15:07
-
-
@maan81 Sadly, there is no way to "store" and "restore" System.out. It's just a stream. Java's console capabilities are *very* limited. – java.is.for.desktop Sep 06 '12 at 22:00
If none of the above solutions works for you( as in my case ), try this solution:
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
I'm using Windows 8 and this solution worked for me. Hope it does to you as well. :)

- 535
- 4
- 10
Clearing a screen generally requires sending special control sequences specific to the screen/terminal that your application is running under. Options:
If you know you will always running under a specific terminal and can find the proper control sequences to clear the screen for that terminal, just output those sequences. If you tell us the screen, we may be able to tell you the sequence (its likely somewhat ANSI/VT100/VT220 -compatible).
Externally ensure your app is always run in a desired terminal, e.g. a script to start your app starts the app in the desired terminal. Then output the necessary character sequence to clear the screen.
Take control of the terminal by using a terminal emulation library, i.e. you app is now a windowing app that creates a terminal window screen for the user to use. You then control what terminal you are emulating and will know what control sequences are needed.
Use a terminal library (e.g. like the historic
curses
library) that detects the terminal and provides an uniform interface to its features. See this question:What's a good Java, curses-like, library for terminal applications?
Fake it by writing a bunch of lines to the screen.
There is always the obvious (and kludgy)..
int numRowsInConsole = 60;
for (int ii=0; ii<numRowsInConsole; ii++) {
// scroll down one line
System.out.println("");
}

- 168,117
- 40
- 217
- 433
System.out.println("Hello!");
prints the specified string and then moves the cursor to the next line.
System.out.print("Hello!");
prints the specified string and leaves the cursor immediately after that string.
To solve the problem, identified above, of the cursor being on the second line of the console, use print
instead of println
.

- 3,724
- 3
- 26
- 33

- 1
- 1
To my knowledge Windows 10's Command Window cmd.exe does not natively support ANSI ESC sequences, despite the rumors. To have the:
final String ANSI_CLS = "\u001b[2J";
System.out.print(ANSI_CLS);
method work you need a command line emulator that supports ANSI, such as ConEmu64.
There are two very simple ways to solve this, the first is the brute force option:
for (int i=1; i<=10; i++)
System.out.println("\n");
The problem with this however is that it only pseudo clears the screen, you can scroll up to see the data, but don't fear, there is another way!
System.out.println("\f");
Voila! That should do the trick, although your cursor will be situated on the second line of the console after the screen is cleared.

- 327
- 1
- 2
- 16