Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"));

I am sure the file is in that directory and it works perfectly in Linux, but it 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 perfectly, also OS X too, the problem is only in Windows.

like image 595
NullPointerException Avatar asked Aug 02 '26 01:08

NullPointerException


1 Answers

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

like image 80
NullPointerException Avatar answered Aug 04 '26 15:08

NullPointerException