I'm trying to run a groovy script, from within a java program, as a separate process (in order to avoid jar collision issues).
This is what I have so far:
public static void runGroovyScript(Path scriptPath, String... args) {
try {
List<String> argsList = newArrayList();
argsList.add("groovy");
argsList.add(scriptPath.toAbsolutePath().toString());
Collections.addAll(argsList, args);
Process process = Runtime.getRuntime().exec(argsList.toArray(new String[argsList.size()]));
// Note - out input is the process' output
String input = Streams.asString(process.getInputStream());
String error = Streams.asString(process.getErrorStream());
logger.info("Groovy output for " + Arrays.toString(args) + "\r\n" + input);
logger.info("Groovy error for " + Arrays.toString(args) + "\r\n" + error);
int returnValue = process.waitFor();
if (returnValue != 0) {
throw new RuntimeException("Groovy process returned " + returnValue);
}
} catch (Throwable e) {
throw new RuntimeException("Failure running build script: " + scriptPath + " " + Joiner.on(" ").join(args), e);
}
}
The problem, of course, is that groovy
is not a recognized command. It works from the command line because of the PATH
environment variable, and the resolving that cmd.exe does. On linux, there is a different resolving mechanism. What is a platform-independent way to find the groovy executable, in order to pass it along to Runtime.exec()
?