To clear the console in Java language, we have multiple methods depending on the environments (like: IDE, terminal, operating system, etc.) In this blog, we will learn how we can clear the console in Java language.
Table of Contents:
Different Methods to Clear the Console Java Console
While working with Java, it might be possible that our console is full of irrelevant messages, so sometimes we need to clear our console. Here, we will learn different methods with examples to clear our console.
Method 1: Using ANSI Escape Code
We can use ANSI Escape codes to clear the console in Java. ANSI escape codes are special character sequences that are interpreted by the terminal to perform various text formatting and screen manipulation tasks.
Code:
System.out.print("\033[H\033[2J"); System.out.flush();
Explanation:
- \033[H: This command moves the cursor to the top-left corner of the screen.
- \033[2J: This command clears the entire console screen.
Method 2: Using the platform-specific command
We can clear the console by using platform-specific commands as well. We can make a ProcessBuilder for our system, whether it is Windows or Linux/Mac, then clear the console with the help of some arguments given in the ProcessBuilder.
- Windows: The cls command clears the console in the Windows Operating system.
- Linux/macOS: The clear command clears the terminal in the Linux or macOS system.
Code:
public class ClearConsole {
public static void main(String[] args) {
System.out.println("Clearing console using ProcessBuilder");
String os = System.getProperty("os.name");
try {
if (os.startsWith("Windows")) {
// Command for Windows
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
// Command for Linux/Mac
new ProcessBuilder("clear").inheritIO().start().waitFor();
}} catch (Exception e) {
System.out.println("Error while clearing console: " + e.getMessage());}
}
}
Method 3: Using the Command Line Interpreter
We can also clear our console by using a command line interpreter. We have to run some commands on our CLI to clear our console.
For Windows:
Runtime.getRuntime().exec("cmd /c cls");
For Linux/macOS:
Runtime.getRuntime().exec("clear");
;
Method 4: Printing Multiple New Lines
When we are working with a Java IDE (Eclipse, IntelliJ, NetBeans), where cls and ANSI code are not working, then we should simply print multiple blank lines, and our console will clear.
Code:
public static void clear() {
for (int i = 0; i < 50; i++) {
System.out.println();
}
}
Explanation: The above code will print 50 blank lines, making it easier to clear the console.
Which Method is Best?
When working with Java, we must know which method is best to clear the console. But it depends on the environment we are using. Let’s see the below table:
Environment | Best Method |
Linux/macOS terminal | ANSI escape codes (\033[H\033[2J) |
Windows Command Prompt (cmd.exe) | ProcessBuilder(“cmd”, “/c”, “cls”) |
PowerShell / Windows Terminal | ANSI escape codes (\033[H\033[2J) |
Java IDEs (Eclipse, IntelliJ, NetBeans) | Printing multiple new lines |
Conclusion
Java is a very powerful, high-level, and object-oriented programming language that is generally used by millions of developers to build and create a wide range of applications.
So far in this article, we have learned three different methods to clear the console in Java. If you want to excel in your career in Java, please refer to our advanced Java course.
FAQs
What is clear() in Java?
The clear() method is used in collections like Set, Map, or List to remove all elements, but it is not a method for clearing the console.
How to clear the console in Java?
Java doesn’t have a built-in method to clear the console, but you can use ANSI escape codes (\033[H\033[2J) or ProcessBuilder to run platform-specific commands like cls or clear.
Is Java easy to learn?
Yes, Java is easy to learn, because of its simpler syntax, beginner-friendly, strong community support, etc.
Can I learn Java in 21 days?
Yes, you can learn Java basics in 21 days with full dedication and consistency.