Reputation: 8067
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.
Upvotes: 1
Views: 102
Reputation: 5046
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"});
Upvotes: 1
Reputation: 33
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.
}
Upvotes: 2