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
user182944
8,10536 gold badges112 silver badges182 bronze badges
2 Answers 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.
}
Sign up to request clarification or add additional context in comments.
Comments
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
Pokechu22
5,0569 gold badges39 silver badges64 bronze badges
4 Comments
user182944
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?Pokechu22
@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?user182944
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?Pokechu22
@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.
lang-java
Run windowthen it works but the same command does not works when tried from acommand prompt. Hence the link provided does not helped me much.