Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Process Builder未在Windows上工作_Java_Imagemagick_Processbuilder - Fatal编程技术网

Java Process Builder未在Windows上工作

Java Process Builder未在Windows上工作,java,imagemagick,processbuilder,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

我正在使用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());
        }
    }
 }
String input=“D:\\Koala.jpg”;
字符串输出=“D:\\ProcessBuilderOutput\\KoalaPNG.png”;
命令。添加(“D:\\Program Files\\ImageMagick-6.8.6-Q16\\convert”);
命令。添加(“-alpha off”);
命令。添加(“-strip”);
命令。添加(输入);
添加(“-colorspace CMYK”);
命令。添加(输出);
试一试{
executeProcessCommand(命令);
如果(新文件(输出).exists()!=true){
System.out.println(“未生成输出”);
}
}捕获(例外e){
e、 printStackTrace();
}
公共静态void executeProcessCommand(列表命令)引发异常{
processproc=null;
试一试{
System.out.println(“-executeProcessCommand:尝试执行:-”+命令);
ProcessBuilder ProcessBuilder=新的ProcessBuilder(命令);
proc=processBuilder.start();
进程waitFor();
System.out.println(“-executeProcessCommand:已执行命令”);
}捕获(例外e){
System.out.println(“-executeProcessCommand:+e.getMessage());
}最后{
试一试{
如果(proc!=null)
proc.destroy();
}捕获(例外e){
System.out.println(“executeProcessCommand:+e.getMessage());
}
}
}

将参数拆分到ProcessBuilder。特别是拆分
命令。添加(“-alpha off”)进入

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

类似地,对于
命令.add(“-colorspace CMYK”)

工作不一致或根本不工作?你的输入文件是什么?“不工作”到底意味着什么?@chrylis,当我尝试了10次时,输出只生成了一次……无论如何,我已经编辑了这个问题……阅读(并实现)了所有的建议。这可能会解决问题。如果没有,它应该提供更多关于失败原因的信息。然后忽略它引用的
exec
,并(继续)使用
ProcessBuilder
构建
流程。还可以将
字符串arg
分解为
字符串[]args
,以说明本身包含空格的参数。