How to clear console in Java – Eclipse

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"); 

Also, you can print multiple end of lines (“\n”) and simulate the clear screen. At the end clear, at most in the unix shell, not removes the previous content, only moves it up and if you make scroll down can see the previous content.

Here is a sample code:

for (int i = 0; i < 50; ++i) System.out.println();

A third option:

import java.io.IOException;

public class CLS {
    public static void main(String... arg) throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    }
}

Leave a Comment