我正在使用Process Builder在Windows上运行ImageMagick命令。出于某种原因,使用Process Builder大多数时间都不会生成输出图像。当我使用Runtime.getRuntime().exec
尝试相同的命令时,生成了输出。知道为什么会这样吗?
String input="D:\\Koala.jpg";
String output = "D:\\ProcessBuilderOutput\\KoalaPNG.png";
commands.add("D:\\Program Files\\ImageMagick-6.8.6-Q16\\convert");
commands.add("-alpha off");
commands.add("-strip");
commands.add(input);
commands.add("-colorspace CMYK");
commands.add(output);
try{
executeProcessCommand(commands);
if(new File(output).exists() != true){
System.out.println("output not generated");
}
}catch (Exception e) {
e.printStackTrace();
}
public static void executeProcessCommand(List<String> commands) throws Exception {
Process proc = null;
try {
System.out.println("-executeProcessCommand: Trying to execute :- "+commands);
ProcessBuilder processBuilder = new ProcessBuilder(commands);
proc = processBuilder.start();
proc.waitFor();
System.out.println("- executeProcessCommand: Executed the command ");
} catch (Exception e) {
System.out.println(" - executeProcessCommand:" + e.getMessage());
} finally {
try {
if(proc != null)
proc.destroy();
} catch (Exception e) {
System.out.println("executeProcessCommand:"+ e.getMessage());
}
}
}
答案 0 :(得分:1)
将参数拆分为ProcessBuilder。特别是将commands.add("-alpha off");
拆分为
commands.add("-alpha");
commands.add("off");
,同样适用于commands.add("-colorspace CMYK");