1

Opening the Run window (Windows + r) and running a command -> I want to trigger this same command using Java. I tried this using :

Runtime.getRuntime().exec(command);

But this did not worked. Please let me know how to achieve this.

user182944
  • 7,197
  • 28
  • 96
  • 170
  • does [this](http://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) and [this](http://www.mkyong.com/java/how-to-execute-shell-command-from-java/) help you – Ankur Singhal Oct 16 '14 at 15:11
  • I checked that link earlier but no, it did not helped me as such. The problem is: If run the command using the `Run window` then it works but the same command does not works when tried from a `command prompt`. Hence the link provided does not helped me much. – user182944 Oct 16 '14 at 15:13

2 Answers2

2

Can you try this:

ProcessBuilder pb=new ProcessBuilder("explorer");
        pb.redirectErrorStream(true);
        Process process=pb.start();
        BufferedReader inStreamReader = new BufferedReader(
            new InputStreamReader(process.getInputStream())); 

        while(inStreamReader.readLine() != null){
            //do something with commandline output.
        }
Stuk4
  • 33
  • 3
1

Use this command:

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "winword"});

This successfully runs Microsoft word (winword), which is not runnable directly through cmd. The start command behaves the same as run does.

Add parameters afterwards like this:

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "winword", "C:\\Example.docx"});
Pokechu22
  • 4,790
  • 8
  • 35
  • 59
  • How to invoke this command: `start demo:" -ping -ip 172.18.102.65"` using the `Runtime exec method`? I tried this: `Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "demo:\" -ping -ip 172.18.102.65\""});` but it did not work. I am getting an error message: `Windows Cannot find -ping' Can you please guide me on this? – user182944 Oct 17 '14 at 16:27
  • @user182944 I have no idea what that command is supposed to do and it doesn't work in run on my computer. Is there a program on your computer named `start demo` that you are trying to run? – Pokechu22 Oct 17 '14 at 16:29
  • In the above command, `demo:` is a `custom uri` which I created and I am passing the parameters to that `custom uri` which are `-ping`, `-ip` and `` to make this custom uri work. Can you please guide? – user182944 Oct 17 '14 at 16:31
  • 1
    @user182944 I'm not too experienced with custom uris. It would probably be best to ask a new question and link back to this question for reference. – Pokechu22 Oct 17 '14 at 16:34