+ 2
How to clear screen?
how to clear screan at the time of execution in java
4 Answers
+ 8
@Faisal This command does not work, for two reasons:
Runtime.getRuntime().exec("cls");
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():
import java.io.IOException;
public class CLS {
public static void main(String... arg) throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} }
Now when the Java process is connected to a console, i.e. has been started from a command line without output redirection, it will clear the console
+ 9
To clean console of your command line application,use following code :
/
*
This method clear the current screen
*
* */
public static void clearScreen() { System.out.print("\033[H\033[2J");
System.out.flush(); }
+ 1
try this:
Runtime.getRuntime().exec("cls");