Execute a command using ProcessBuilder from windows

Bibin picture Bibin · Jan 20, 2014 · Viewed 14.5k times · Source

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

Answer

Franky picture Franky · Jan 20, 2014

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