Three Methods to Clear Screen in Java

February 07, 2022

Tags: Technologies
java
Unsplash

 

Anyone who is dedicated to the development of applications and web pages has a basic knowledge of Java, perhaps the most popular programming language in the world. The definition given on its official website is as follows: “Java is a programming language and computing platform that was first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power much of today's digital world, providing a reliable platform on which to build. they build many services and applications.”

 

What is Java used for?

 

Being a multiplatform programming language, Java has been given several uses, among these the following stand out:

 

  • It is used to develop Android applications.
  • It is one of the most popular for creating business software.
  • There is a wide range of mobile Java applications on the market
  • Scientific computing applications
  • Use for Big Data analysis
  • Java programming of hardware devices
  • It is used for server-side technologies like Apache, JBoss, GlassFish, etc.

 

How to clear the screen in Java

 

In order to clean the screen in Java, there are three methods that are the most used by expert developers in this language:

 

  1. Using the ANSI escape code
  2. Using the platform-specific command
  3. Using the command-line interpreter

 

1. Using the ANSI escape code

 

This ANSI escape sequence can be defined as standard in-band signaling to control cursor position. In this tutorial, the escape code \033[H\033[2J will be used. Explained separately:

 

\033: Represents the ASCII escape character. Its ANSI value is 27. It means ESC.
[: Represents the escape sequence.

 

Combining the above codes, we get \033[ or ESC[.

 

public class ClearScreenExample1
{
public static void main(String[] args)
{
System.out.print("\033[H\033[2J");
System.out.flush();
}
}

 

2. Using the platform-specific command

 

Another popular method to clean the console in Java is the command offered by the platform we are using. To do this, you first get the system property using getProperty() of the System class. Then the command used on the platform to clear the console is selected.

 

System.getProperty() method

 

This method is used to get the system property indicated by the specified key. In syntax it looks like this:

 

public static String getProperty(String key)

 

Now let's create a program to clean up the console in Java using the platform-specific command:

 

public class ClearScreenExample2
{
public final static void clearConsole()
{
public static void main(String[] args)
{
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
}
catch (final Exception e)
{
e.printStackTrace();
}
}

 

3. Using the command line interpreter

 

With this method, the command line interpreter, CMD for its acronym in English, is invoked. After doing this, the interpreter executes the cls command. It can be done using the heritageIO() method.

 

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

 

Thus, with these three simple methods, the developer will be able to clean the console in Java.

 

We recommend you on video

 

Yes, I liked thiscontent.