I am developing a plugin for intelliJ.
I need to call some mvn
commands inside my java code. But strangely, it returns me an IOexception
:
Cannot run program "mvn" (in directory "/Users/ryouyasachi/Getty/semantiful-differentials-getty/getty/dsproj"):
error=2, No such file or directory
This is my code:
/** @return: null if the process does not exit with 0
* @return: output if the process exits with 0
*/
public static String runCommand(String directory, List<String> command) {
ProcessBuilder processBuilder = new ProcessBuilder(command).directory(new File(directory));
processBuilder.redirectErrorStream(true);
Process process;
String output = null;
try {
process = processBuilder.start();
//Pause the current thread until the process is done
process.waitFor();
//When the process does not exit properly
if (process.exitValue() != 0) {
//Error
System.out.println("command exited in error: " + process.exitValue());
//Handle the error
return null;
}else {
output = readOutput(process);
System.out.println(output);
}
} catch (InterruptedException e) {
System.out.println("Something wrong with command: " +e.getMessage());
} catch (IOException e) {
System.out.println("Something wrong with command: " +e.getMessage());
}
return output;
}
/**
*
* @param process which exits with 0
* @return The output string of the process
*/
private static String readOutput(Process process){
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder stringBuilder = new StringBuilder();
try {
while ((line = in.readLine()) != null) {
stringBuilder.append(line);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
PS: 1. I am able to run the mvn commands I need in the prompt under the project directory.This should indicate that the project directory does exist and I have maven installed properly.
2.My code runs fine with the git
commands.