Java – How to execute a batch file in Windows in Cygwin shell

batch-filecygwinjava

I am trying to execute a BATCH file through through a jsp on a Windows machine and it works fine without any problems.

However DOS does not have an equivalent of sed unix command. Hence I had to install cygwin so that I can use the the following sed command in my batch file.

sed -i "s/<model.version>2.1.1-SNAPSHOT<\/model.version>/<model.version>2.1.1-b-1<\/model.version>/g" pom.xml  

My question is that how can I invoke the cygwin shell so that my .bat file runs within cygwin shell rather than windows command prompt.

Following is the piece of code that works fine. The only problem is that it invokes command prompt. I want to invoke cygwin shell instead of command prompt.

File dir = new File("C:\\Projects\\release-builds");  
Runtime.getRuntime().exec("cmd.exe /c start build-model.bat", null, dir);  

Please advise.

Best Answer

You need to add the cygwin directory to your PATH. (or the PATH of your service) then you can run cygwin commands from a DOS prompt or bat script.

Or you can give the full path for your c:\cygwin\usr\bin\sed.exe or whatever it is.