首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProcessBuilder诉Runtime.exec()

ProcessBuilder诉Runtime.exec()
EN

Stack Overflow用户
提问于 2010-10-22 13:14:33
回答 1查看 18.5K关注 0票数 9

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

CallerTest.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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-方法B.png失败

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

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

Runtime.exec(String)说,JavaDoc调用Runtime.exec(命令,null) (它是Runtime.exec( String[],String[] envp)),后者依次调用Runtime.exec(cmdarray,envp) (即Runtime.exec(String[] cmdarray,String[] envp))。那么,如果Runtime.getRuntime().exec(String)无论如何都在调用Runtime.exec(String[]),为什么我在使用不同的方法时得到不同的结果?

在Java以不同方式设置环境的幕后是否发生了一些事情,这取决于调用哪个方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-22 14:04:19

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

您需要分别传递参数,如下所示:

代码语言:javascript
复制
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进行计算,该shell将解析参数列表。当您使用Runtime.exec(String[])时,您将提供参数列表,因此它不需要处理。这样做的一个好处是不必转义shell特有的值,因为参数不会被它计算。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3997127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档