ch.cern.mig.utils.ProcessUtils Maven / Gradle / Ivy
/**
*
*/
package ch.cern.mig.utils;
import java.io.IOException;
import java.lang.InterruptedException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Generic process utiitiess.
*
* @author Massimo Paladin - [email protected]
* Copyright (C) CERN 2012-2013
*/
public class ProcessUtils {
/**
* Execute the given system command and return a Map containing output
* results and exit value.
*
* @param command
* @return Map containing output results and exit value
*/
public static Map executeIt(String command) {
StringBuilder output = new StringBuilder();
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
Scanner sc = new Scanner(process.getInputStream());
process.waitFor();
while (sc.hasNext()) {
output.append(sc.nextLine());
}
} catch (IOException e) {
output.append(e.getMessage());
} catch (InterruptedException e) {
output.append(e.getMessage());
}
Map result = new HashMap();
result.put("exitValue", "" + process.exitValue());
result.put("out", output.toString());
return result;
}
}