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.

asked Oct 16, 2014 at 15:05
2
  • does this and this help you Commented Oct 16, 2014 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. Commented Oct 16, 2014 at 15:13

2 Answers 2

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.
 }
answered Oct 16, 2014 at 15:21
Sign up to request clarification or add additional context in comments.

Comments

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"});
answered Oct 16, 2014 at 15:22

4 Comments

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 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?
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 <an_ip_address> to make this custom uri work. Can you please guide?
@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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.