tags:

views:

48

answers:

2

I was able to open a command prompt from my Java program with the following code:

String cmd = "C:\\WINNT\\system32\\cmd.exe /c start";


    try {
        @SuppressWarnings("unused")
        Process ps = Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        e.printStackTrace();
    }

The above code opens the command prompt.

If I want to execute some command in this opened command prompt , what I have to do?

ANy help is appreciated.

A: 

I know that cmd /k [some other command] will run that command in the command prompt, but it only runs one, so it is a limited solution

murgatroid99
I did n't understand. DO you mean that the opened cmd prompt is of no use once it is opened. Should I try directly executing command with out opening it?
If you want the program to execute a single command in the command prompt, then you use `cmd /k`. If you want to use the command prompt as a user, you should be able to just click into it and type commands as usual.
murgatroid99
+1  A: 

I think you are at the right direction. To execute some commands or more than one command, repeat the cmd /k [command], like this:-

// write dir output to file
Runtime.getRuntime().exec(new String[] {
        "cmd",
        "/k",
        "dir",
        ">",
        "c:\\output.txt"
});

// create test-dir folder in c:\
Runtime.getRuntime().exec(new String[] {
        "cmd",
        "/k",
        "mkdir",
        "c:\\test-dir"
});
limc
Thanks limc, but I want ot see the rest of the commands being executed in the opened console. IS that possible.. what if we use ProcessBuilder