0

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,

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by nomemory
0

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)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.