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
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
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();
/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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With