HOWTO · Java
How to Clear the Console in Java
Learn how to clear the console in Java with two effective methods: using ANSI escape codes and executing system commands. Enhance your coding experience and streamline your workflow by implementing these techniques. This article provides clear code examples and detailed explanations for each method, ensuring you can easily manage your console output in Java applications.
On this page
Java has no built-in method that clears a console. During a program run, you can print ANSI escape sequences or start a command provided by the operating system. Which approach works depends on the terminal or IDE that displays the program. Both methods ask the display to clear its visible area; neither can guarantee that older output is removed from scrollback or from an IDE’s stored run history.
Console handling is one of the everyday utilities collected in the Java Tutorial.
Use ANSI Escape Sequences to Clear the Console
ANSI sequences are control characters interpreted by a terminal. The sequence \033[H moves the cursor to the home position, and \033[2J erases the visible screen. Print both before the next prompt or status message. \033[2J erases the entire display; cursor-relative erase behavior is provided by other erase-display parameters, such as \033[0J, which erases from the cursor to the end of the display.
Run this example:
public class ClearConsole {
public static void main(String[] args) {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("Console cleared!");
}
}
[H[2JConsole cleared!
The Java string escapes \033 insert the ESC character. System.out.flush() sends the sequence promptly; without it, buffered output may not reach the terminal before later output. This code sends the request, but it cannot make an unsupported terminal interpret it. ANSI support is common in terminal emulators, but can depend on terminal settings, IDE configuration, and how the program is launched. Clearing the visible screen may also leave the terminal’s scrollback history intact. If the escape characters appear literally or the screen does not change, use the IDE’s clear-console command or check its ANSI support setting.
Run an Operating-System Command with ProcessBuilder
If the terminal provides a clear-screen command, Java can start it with ProcessBuilder. Windows Command Prompt uses cls through cmd /c; macOS and other Unix-like systems commonly provide clear. The example lowercases the OS name with Locale.ROOT and checks for the windows prefix, so macOS (Darwin) follows the Unix-like branch. Select the command for the actual operating system instead of assuming every non-Windows system has the same command. This sample first checks for an interactive console, because a command that clears a terminal cannot clear a file or an IDE pane that is not attached to one:
import java.io.IOException;
import java.util.Locale;
public class ClearConsoleWithCommand {
public static void main(String[] args) {
if (System.console() == null) {
System.out.println("No interactive console is attached.");
return;
}
try {
String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
ProcessBuilder builder = os.startsWith("windows")
? new ProcessBuilder("cmd", "/c", "cls")
: new ProcessBuilder("clear");
builder.inheritIO();
Process process = builder.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
System.out.println("The clear command returned " + exitCode + ".");
}
} catch (IOException e) {
System.out.println("The clear command could not be started.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Waiting for the clear command was interrupted.");
}
}
}
ProcessBuilder starts a separate process; it does not make a command available when that command is absent or outside the process’s search path. inheritIO() connects the child to the same standard input, output, and error streams as the Java process, allowing a terminal command to affect the attached display. The example waits for completion and reports a nonzero exit code. It also restores the interrupted status if waiting is interrupted. A command can still fail when it is missing, the environment lacks terminal information, or the process is launched without a usable interactive terminal. The operating-system check only selects a likely command; it does not prove that the command exists or that the terminal will honor it.
Check Terminal and Operating-System Support
Use the ANSI approach when the terminal is known to interpret ANSI control sequences. It avoids an external dependency and is suitable when the terminal’s behavior is controlled by your application environment. Use the process approach only when the selected command exists and can access the same interactive terminal; IDE run panes, redirected output, and background jobs may not behave like a terminal. System.console() can be null in IDEs and when input or output is redirected, even if an IDE shows a console-like panel. Neither approach reliably removes scrollback or clears every IDE’s console view. If the display remains unchanged, check the terminal’s ANSI setting and whether the program is attached to an interactive terminal, then prefer the IDE’s own clear-console control when you need to clear its view.