+ 2

How to clear screen?

how to clear screan at the time of execution in java

17th Jan 2018, 11:47 AM
Koushik.S.N Sudi
Koushik.S.N Sudi - avatar
3 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
17th Jan 2018, 12:23 PM
+ 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(); }
17th Jan 2018, 12:07 PM
+ 1
try this: Runtime.getRuntime().exec("cls");
17th Jan 2018, 12:11 PM
Faisal Rahman
Faisal Rahman - avatar