* @param file an executable or a batch file
*
* @return a process execution
*/
public static ProcessExecution createProcessExecution(String prefix, File file) {
ProcessExecution processExecution;
List<String> prefixArgs;
if (prefix != null) {
// TODO (ips, 04/27/10): Ideally, the prefix should be a String[], not a String.
prefixArgs = Arrays.asList(prefix.split("[ \t]+"));
} else {
prefixArgs = Collections.emptyList();
}
String executable;
List<String> args = new ArrayList<String>();
if (OS_IS_WINDOWS && isBatchFile(file)) {
// Windows batch files cannot be executed directly - they must be passed as arguments to cmd.exe, e.g.
// "C:\Windows\System32\cmd.exe /c C:\opt\jboss-as\bin\run.bat".
executable = getCmdExeFile().getPath();
args.add("/c");
args.addAll(prefixArgs);
args.add(file.getPath());
} else {
// UNIX
if (prefixArgs.isEmpty()) {
executable = file.getPath();
} else {
executable = prefixArgs.get(0);
if (prefixArgs.size() > 1) {
args.addAll(prefixArgs.subList(1, prefixArgs.size()));
}
args.add(file.getPath());
}
}
processExecution = new ProcessExecution(executable);
processExecution.setArguments(args);
// Start out with a copy of our own environment, since Windows needs
// certain system environment variables to find DLLs, etc., and even
// on UNIX, many scripts will require certain environment variables
// (PATH, JAVA_HOME, etc.).
// TODO (ips, 04/27/12): We probably should not just do this by default.
Map<String, String> envVars = new LinkedHashMap<String, String>(System.getenv());
processExecution.setEnvironmentVariables(envVars);
// Many scripts (e.g. JBossAS scripts) assume their working directory is the directory containing the script.
processExecution.setWorkingDirectory(file.getParent());
return processExecution;
}