Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a command using ProcessBuilder from windows

I am getting the following error when i try to execute the below line from my java program on windows machine.

Could you please let me know the detailed steps to make that work?

final Process exec = new ProcessBuilder("bash", "-c", query).start();

error : java.io.IOException: Cannot run program "bash": CreateProcess error=2, The system cannot find the file specified

like image 859
Bibin Avatar asked Jan 20 '14 10:01

Bibin


3 Answers

Windows has no bash, so you have to use "CMD" (command). "bash" is being used for unix-systems.

This should work on Windows :

final Process exec = new ProcessBuilder("CMD", "/C", query).start();

if you want a nice example on how to use the ProcessBuilder in Windows : External programs using Java ProcessBuilder class

like image 87
Franky Avatar answered Oct 09 '22 20:10

Franky


final Process exec = new ProcessBuilder("bash", "-c", query).start();

As the error indicates, there is no executable program bash, typically bash in installed on Unix systems at location /bin/bash, so you must provide the path to your program. Even relative paths work. This command below will work on Unix like OS with bash installed.

final Process exec = new ProcessBuilder("/bin/bash", "-c", query).start();
like image 36
Nitish Avatar answered Oct 09 '22 21:10

Nitish


/bin/bash doesn't exist on Windows. Try replacing /bin/bash with cmd.exe, and replacing the switch -c with /c

final Process exec = processBuilder("cmd.exe", "/c", query).start();

like image 1
Amulya M Avatar answered Oct 09 '22 19:10

Amulya M