Process Builder无法在Windows上运行

时间:2014-01-05 06:57:29

标签: java imagemagick processbuilder

我正在使用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());
        }
    }
 }

1 个答案:

答案 0 :(得分:1)

将参数拆分为ProcessBuilder。特别是将commands.add("-alpha off");拆分为

commands.add("-alpha");
commands.add("off");

,同样适用于commands.add("-colorspace CMYK");