Hi all,
I am trying to execute a windows command from within my java program, but I can't get it to execute the DOS command. Here's my code:
try { Process p = Runtime.getRuntime().exec("cmd.exe /C cls"); } catch (IOException e) { // catch exception }
But, the code doesn't clear the screen, nor it gives any error. Can you please let me know, what I am doing wrong here?
Thanks,
What are you expecting it to do? That command isn't going to produce any noticeable output.
Try to create the process using the ProcessBuilder class:
ProcessBuilder pb= new ProcessBuilder(command); Process proc= pb.start();
"command" is a list of Strings. For Windows it should look like this:
String[] command= { "cmd", "/C" , "dir" };
Additional: After you create the output, you can obtain a stream of data from the process object itself:
Scanner sc= new Scanner(proc.getInputStream());
(if you want to wrap the stream inside a scanner)