The String passed to the exec
method is automatically parsed to define the parameters of the command. Since your path contain spaces (and might contain special characters too), you should consider using ProcessBuilder to construct your command.
Moreover, the constructor of the ProcessBuilder is the command to execute but you can also change the working directory using the directory method.
try {
String[] cmd = {"cmd",
"/c",
"gradlew",
"assembleRelease"};
ProcessBuilder pb = new ProcessBuilder(cmd);
// Change working directory
pb.directory(new File("C:\\Users\\CA_LTD\\AndroidStudioProjects\\AMBITION"));
// Run command
Process p = pb.start();
// Wait for completion
int exitValue = p.waitFor();
// Check exit value
if (exitValue != 0) {
// TODO: Define your behaviour when process exits with non-zero exit value
// Print command output
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream());
String outLine = null;
while ( (outLine = reader.readLine()) != null) {
System.out.println(outLine);
}
// Print command error
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream());
String errLine = null;
while ( (errLine = reader.readLine()) != null) {
System.err.println(errLine);
}
// Throw exit value exception
throw new Exception("ERROR: CMD process exited with non-zero value: " + exitValue);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
If you don't want to check the exit value of the command (in Windows the exit value is a pseudo environment variable named errorlevel
as described in here ), you can just do:
try {
String[] cmd = {"cmd",
"/c",
"gradlew",
"assembleRelease"};
ProcessBuilder pb = new ProcessBuilder(cmd);
// Change working directory
pb.directory(new File("C:\\Users\\CA_LTD\\AndroidStudioProjects\\AMBITION"));
// Run command
Process p = pb.start();
// Wait for completion
p.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
}