Java: Clear the console -
can body please tell me code used clear screen in java? example in c++
system("cls");
what code used in java clear screen?
thanks!
since there several answers here showing non-working code windows, here clarification:
runtime.getruntime().exec("cls");
this command not work, 2 reasons:
there no executable named
cls.exe
orcls.com
in standard windows installation invoked viaruntime.exec
, well-known commandcls
builtin windows’ command line interpreter.when launching new process via
runtime.exec
, standard output gets redirected pipe initiating java process can read. when output ofcls
command gets redirected, doesn’t clear console.
to solve problem, have invoke command line interpreter (cmd
) , tell execute command (/c cls
) allows invoking builtin commands. further have directly connect output channel java process’ output channel, works starting 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 java process connected console, i.e. has been started command line without output redirection, clear console.
Comments
Post a Comment