Java – Runtime.getRuntime().exec -> Cannot run program CreateProcess error=2, The system cannot find the file specified

command lineexecjavaruntimewindows

I'm developing a command line java app that must run a program called gradlew.bat assembleRelease inside this directory: this.workDir+"/Project/CapAndroid"

So i did this:

Process p = Runtime.getRuntime().exec("gradlew.bat assembleRelease", null , new File(this.workDir+"/Project/CapAndroid"));

The file is in that directory 100% sure and it works perfect in linux but did not work on Windows! I got this error:

java.io.IOException: Cannot run program "gradlew.bat" (in directory
"C:\Users\Administrador\Desktop\generators\And\jobs\2247994\Project\CapAndroid"):
CreateProcess error=2, The system cannot find the file specified

I think windows has a problem to know that the command passed in the first parameter of exec() method must be executed in the directory passed in the last parameter. Linux works perfect, also OS X too, the problem is only in Windows

Thanks a lot

Best Answer

Well, i finally solved it adding cmd /c before the name of the .bat file, now it works perfectly:

Process p = Runtime.getRuntime().exec("cmd /c gradlew.bat assembleRelease", null , new File(this.workDir+"/Project/CapAndroid"));

i hope this will help someone in the future

Related Topic