Как очистить консоль в java
Перейти к содержимому

Как очистить консоль в java

  • автор:

Clear the Console in Java

Clear the Console in Java

We have introduced how to get input from console in Java in another article. In this tutorial, we will look at the two ways that can be used to clean the console screen in Java. We will be looking at examples to learn how to execute Java clear screen commands at runtime.

Use ANSI Escape Codes to Clear Console in Java

Please enable JavaScript

We can use special codes called ANSI escape code sequences to change cursor positions or display different colors. These sequences can be interpreted as commands that are a combination of bytes and characters.

To clear the console in Java, we will use the escape code \033[H\033[2J . This weird set of characters represents the command to clean the console. To understand it better, we can break it down.

The first four characters \033 means ESC or the escape character. Combining 033 with [H , we can move the cursor to a specified position. The last characters, 033[2J , cleans the whole screen.

We can look at the below example, which is using these escape codes. We are also using System.out.flush() that is specially used for flushing out the remaining bytes when using System.out.print() so that nothing gets left out on the console screen.

Use ProcessBuilder to Clear Console in Java

In this method, we will use a ProcessBuilder that is a class mainly used to start a process. We can build a process with the commands that will clean the console.

ProcessBuilder() takes in the commands to execute and its arguments. The issue with this approach is that different operating systems can have different commands to clean the console screen. It is why, in our example, we check the current operating system.

At last, we are using the Process class to start a new process with inheritIO to set the standard input and output channels to Java’s I/O channel.

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

How to clear the console using Java?

Can any body please tell me what code is used for clear the screen in Java?

For example, in C++:

What code is used in Java to clear the screen?

Ola Ström's user avatar

14 Answers 14

Since there are several answers here showing non-working code for Windows, here is a clarification:

This command does not work, for two reasons:

There is no executable named cls.exe or cls.com in a standard Windows installation that could be invoked via Runtime.exec , as the well-known command cls is builtin to Windows’ command line interpreter.

When launching a new process via Runtime.exec , the standard output gets redirected to a pipe which the initiating Java process can read. But when the output of the cls command gets redirected, it doesn’t clear the console.

To solve this problem, we have to invoke the command line interpreter ( cmd ) and tell it to execute a command ( /c cls ) which allows invoking builtin commands. Further we have to directly connect its output channel to the Java process’ output channel, which works starting with Java 7, using inheritIO() :

How to clear the console in Java

  • All categories
  • ChatGPT (11)
  • Apache Kafka (84)
  • Apache Spark (596)
  • Azure (145)
  • Big Data Hadoop (1,907)
  • Blockchain (1,673)
  • C# (141)
  • C++ (271)
  • Career Counselling (1,060)
  • Cloud Computing (3,469)
  • Cyber Security & Ethical Hacking (162)
  • Data Analytics (1,266)
  • Database (855)
  • Data Science (76)
  • DevOps & Agile (3,608)
  • Digital Marketing (111)
  • Events & Trending Topics (28)
  • IoT (Internet of Things) (387)
  • Java (1,247)
  • Kotlin (8)
  • Linux Administration (389)
  • Machine Learning (337)
  • MicroStrategy (6)
  • PMP (423)
  • Power BI (516)
  • Python (3,193)
  • RPA (650)
  • SalesForce (92)
  • Selenium (1,569)
  • Software Testing (56)
  • Tableau (608)
  • Talend (73)
  • TypeSript (124)
  • Web Development (3,002)
  • Ask us Anything! (66)
  • Others (2,231)
  • Mobile Development (395)
  • UI UX Design (24)

Как очистить консоль в java

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

Read and write operations are synchronized to guarantee the atomic completion of critical operations; therefore invoking methods readLine() , readPassword() , format() , printf() as well as the read, format and write operations on the objects returned by reader() and writer() may block in multithreaded scenarios.

Invoking close() on the objects returned by the reader() and the writer() will not close the underlying stream of those objects.

The console-read methods return null when the end of the console input stream is reached, for example by typing control-D on Unix or control-Z on Windows. Subsequent read operations will succeed if additional characters are later entered on the console’s input device.

Unless otherwise specified, passing a null argument to any method in this class will cause a NullPointerException to be thrown.

Security note: If an application needs to read a password or other secure data, it should use readPassword() or readPassword(String, Object. ) and manually zero the returned character array after processing to minimize the lifetime of sensitive data in memory.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *