ProcessBuilder与Runtime.exec()

时间:2010-10-22 13:14:34

标签: java external-process runtime.exec processbuilder inkscape

我正在尝试使用Inkscape的命令行功能在Java中创建一个前端应用来处理批量SVG转换。我正在从https://sourceforge.net/projects/conversionsvg/获取并更新代码。原始开发人员通过 Runtime.getRuntime()。exec(String)处理调用Inkscape的方式。我遇到的问题是使用methodA和methodB之间存在一些不一致。我创建了一个简单的java测试项目来演示正在执行的不同操作。

CallerTest.java

package conversion;

import java.io.IOException;

public class CallerTest {

    static String pathToInkscape = "\"C:\\Program Files\\Inkscape\\inkscape.exe\"";  

    public static void main(String[] args) {

      ProcessBuilderCaller processBuilder = new ProcessBuilderCaller();
      RuntimeExecCaller runtimeExec = new RuntimeExecCaller();

      // methodA() uses one long command line string
      try {

        String oneLongString_ProcessBuilder = pathToInkscape + " -f \"C:\\test.svg\" -D -w 100 -h 100 -e \"C:\\ProcessBuilder-methodB.png\"";
        String oneLongString_RuntimeExec =    pathToInkscape + " -f \"C:\\test.svg\" -D -w 100 -h 100 -e \"C:\\RuntimeExec-methodA.png\"";

//        processBuilder.methodA(oneLongString_ProcessBuilder);
        runtimeExec.methodA(oneLongString_RuntimeExec);

      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      // methodB() uses an array containing the command and the options to pass to the command
      try {

        String[] commandAndOptions_ProcessBuilder = {pathToInkscape, " -f \"C:/test.svg\" -D -w 100 -h 100 -e \"C:\\ProcessBuilder-methodB.png\""};
        String[] commandAndOptions_RuntimeExec =    {pathToInkscape, " -f \"C:/test.svg\" -D -w 100 -h 100 -e \"C:\\RuntimeExec-methodB.png\""};

        processBuilder.methodB(commandAndOptions_ProcessBuilder);
//        runtimeExec.methodB(commandAndOptions_RuntimeExec);

      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
}

RuntimeExecCaller.java

package conversion;

import java.io.IOException;

public class RuntimeExecCaller {
    Process process;

    // use one string
    public void methodA(String oneLongString) throws IOException {
      process = Runtime.getRuntime().exec(oneLongString);
    }

    // use the array
    public void methodB(String[] commandAndOptions) throws IOException {
      process = Runtime.getRuntime().exec(commandAndOptions);
    }
}

ProcessBuilderCaller.java

package conversion;

import java.io.IOException;

public class ProcessBuilderCaller {
    Process process;

    // use one string
    public void methodA(String oneLongString) throws IOException {
      process = new ProcessBuilder(oneLongString).start();
    }

    // use the array
    public void methodB(String[] commandAndOptions) throws IOException {
      process = new ProcessBuilder(commandAndOptions).start();
    }
}

结果

两个 methodA(String)调用都有效,但是当调用 methodB(String [])时,正在启动Inkscape并且参数传递不正确。在 methodB(String [])执行后,我得到每个说法的Inkscape错误对话框

  

无法加载请求的文件-f C:/test.svg -D -w 100 -h 100 -e C:\ RuntimeExec-methodB.png

     

无法加载请求的文件-f C:/test.svg -D -w 100 -h 100 -e C:\ ProcessBuilder-methodB.png

当我在对话框中单击“关闭”时,Inkscape会弹出一个新的空白文档。所以,我想我有几个问题:

Runtime.getRuntime()。exec(String)和Runtime.getRuntime()。exec(String [])之间有什么区别?

JavaDoc说 Runtime.exec(String) 调用 Runtime.exec(command,null) Runtime.exec(String cmd,String [] envp) )又调用 Runtime.exec(cmdarray,envp) Runtime.exec(String [] cmdarray,String [] envp) )。所以,如果 Runtime.getRuntime()。exec(String)正在调用 Runtime.exec(String []),为什么我在使用不同的方法时会得到不同的结果?

在幕后发生的事情是,根据调用的方法,Java会以不同的方式设置环境吗?

1 个答案:

答案 0 :(得分:12)

我怀疑你的问题源于你指定参数列表的方式。基本上,您将“-f C:/test.svg -D -w 100 -h 100 -e C:\RuntimeExec-methodB.png”作为一个参数传递给Inkscape。

您需要做的是单独传递参数,如下所示:

String[] commandAndOptions_ProcessBuilder = {pathToInkscape, "-f", "C:\\est.svg", "-D", "-w", "100", "-h", "100", "-e", "C:\\ProcessBuilder-methodB.png"};
String[] commandAndOptions_RuntimeExec = {pathToInkscape, "-f", "C:\\test.svg", "-D", "-w", "100", "-h", "100", "-e","C:\\RuntimeExec-methodB.png"};

粗略地说,当你使用Runtime.exec(String)时,你传入的值会被shell评估,它会解析出参数列表。当您使用Runtime.exec(String[])时,您将提供参数列表,因此不需要处理。这样做的好处是你不必转义shell特有的值,因为参数不会被它评估。