Software Testing & QA Services

Using Platform-Specific Command to Clear Screen in Java

October 08, 2024

0
Tags: Technologies
clear screen in java

 

In console applications, a common functionality that some developers look to implement is the ability to clear the screen during program execution. Although Java does not offer a native command to clear the console screen, it is possible to achieve this effect using platform-specific commands. 

 

While this approach is not as straightforward as in other languages ​​such as Python or C, it is important to understand that Java is designed to be a platform-independent language. Therefore, the implementation of commands to clear the screen varies depending on the operating system on which the program is running. Here we show you how to tackle this challenge on different operating systems.

 

Screen Clearing in Windows

 

To clear the console in a Windows environment, we can make use of the cls command, which is native to the operating system. In Java, the way to execute this type of command is through the Runtime class that allows us to interact with the operating system and execute commands from the command line.

 

Here is an example of how you can clear the screen in Windows from a Java program:

 

public class ClearScreen {
public static void clearConsole() {
try {
if (System.getProperty("os.name").contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
} catch (Exception e) {
System.out.println("Error trying to clear console.");
}
}
public static void main(String[] args) {
System.out.println("Text on screen before clearing.");
clearConsole();
System.out.println("Screen cleared.");
}
}

 

Code explanation:

 

  • We use System.getProperty("os.name") to detect if the program is running on Windows.
  • If so, ProcessBuilder is used to run the cmd /c cls command which clears the Windows console.
  • The inheritIO() method ensures that the process output is the same as the current console output.
  • This code is quite efficient, but it is not universal as it will not work on Unix-based operating systems such as Linux or macOS.

 

how to clear screen in java

 

Clearing Screen on Linux and macOS

 

On Unix-based systems such as Linux or macOS, the equivalent command to cls is clear. For these environments, the same approach as on Windows can be used, but changing the command to be executed. Here is a modified example to work on Unix systems:

 

public class ClearScreen {
public static void clearConsole() {
try {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("linux") || os.contains("mac")) {
new ProcessBuilder("clear").inheritIO().start().waitFor();
} else if (os.contains("windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
} catch (Exception e) {
System.out.println("Error while trying to clear the console.");
}
}
public static void main(String[] args) {
System.out.println("Text on screen before clearing.");
clearConsole();
System.out.println("Screen cleared.");
}
}

 

Code explanation:

 

  • We use System.getProperty("os.name").toLowerCase() to get the name of the operating system and evaluate it.
  • If the system is Linux or macOS, the clear command is executed, while on Windows, cls is executed.

 

Limitations of the approach

 

It is important to note some limitations of this method:

 

  • OS dependency: This method is not cross-platform by default. You must detect the operating system and use the appropriate command, as we have shown.
  • Not applicable to all console interfaces: Not all Java runtimes allow executing console commands in this way. For example, some IDEs (such as Eclipse) or embedded consoles in graphical applications might not allow executing system commands.
  • Performance: Calling external processes may have a minimal impact on application performance, although in most cases it is a negligible impact.

 

Alternative: Simulate screen clearing

 

Another way to simulate screen clearing is to simply print several blank lines to "scroll" the previous console content out of view. While not a perfect method, it is a simple, cross-platform solution that can work in some cases.

 

public class ClearScreen {
public static void clearConsole() {
for (int i = 0; i < 50; i++) {
System.out.println();
}
}
public static void main(String[] args) {
System.out.println("Text on screen before clearing.");
clearConsole();
System.out.println("Screen simulated as clearing.");
}
}

 

This method simply prints 50 blank lines, scrolling the visible console content up, but not actually clearing it. While not a true "cleanup", it may be sufficient in environments where system commands cannot be executed.

 

Clearing the screen in Java is not something that can be achieved directly with a language function, but we can use operating system-specific commands to do it effectively. On both Windows and Linux/macOS, the key is to detect the operating system and run the appropriate command (cls or clear), using the ProcessBuilder class. It is also possible to simulate a screen clearing by printing several blank lines, a simple but less effective solution.

 

If you need further support or want to optimize your Java development with better implementation practices, our agency is here to help. Contact us for customized and efficient solutions that fit your project needs.

 

We recommend you on video