[Java] I want to do something like cls in Java

less than 1 minute read

A program when you want to do something like cls on Windows or clear on Linux in Java.

You can clear the screen by using the escape sequence.

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

There is an output before clearing when scrolling.
You can also clear the screen by executing it as an external process as shown below.
In this case, scrolling has no previous output.

// Windows
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
// Linux
new ProcessBuilder("clear").inheritIO().start().waitFor();

Tags:

Updated: