Java プログラム内から別のプロセスとして (jar の衝突の問題を回避するために) グルーヴィーなスクリプトを実行しようとしています。
これは私がこれまでに持っているものです:
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);
}
}
もちろん、問題はgroovy
認識されたコマンドではないことです。PATH
環境変数と cmd.exe の解決により、コマンド ラインから動作します。Linux では、別の解決メカニズムがあります。に渡すために、プラットフォームに依存しない Groovy 実行可能ファイルを見つける方法は何Runtime.exec()
ですか?