【问题标题】:java getRuntime().exec() not working?java getRuntime().exec() 不工作?
【发布时间】:2012-07-26 11:35:23
【问题描述】:

基本上,当我在 手动终端,sift 程序工作并写入一个 .key 文件,但是当我尝试从我的程序中调用它时,没有写入任何内容。

我是否正确使用了 exec() 方法?我查看了 API,但似乎找不到哪里出错了。

public static void main(String[] args) throws IOException, InterruptedException
{           
        //Task 1: create .key file for the input file
        String[] arr  = new String[3];
        arr[0] =  "\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/siftWin32.exe\"";
        arr[1] = "<\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/cover_actual.pgm\"";
        arr[2] = ">\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/keys/cover_actual.key\"";

        String command = (arr[0]+" "+arr[1]+" "+arr[2]);

        Process p=Runtime.getRuntime().exec(command); 
        p.waitFor(); 
        BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
        String line=reader.readLine(); 

        while(line!=null) 
        { 
            System.out.println(line); 
            line=reader.readLine(); 
        } 
}

【问题讨论】:

  • 你得到了什么错误??
  • 我没有收到任何错误,但它也没有像预期的那样写入 .key 文件。
  • 您确定可以将输出重定向与Runtime.exec 一起使用吗?

标签: java runtime exec


【解决方案1】:

您不能将重定向(&lt;&gt;)与 Runtime.exec 一起使用,因为它们是由 shell 解释和执行的。它仅适用于一个可执行文件及其参数。

进一步阅读:

【讨论】:

    【解决方案2】:

    您使用的命令行是 DOS 命令行,格式为:

    prog < input > output
    

    程序本身不带参数执行:

    prog
    

    但是您的代码中的命令执行为

    prog "<" "input" ">" "output"
    

    可能的修复:

    a) 使用 Java 处理输入和输出文件

    Process process = Runtime.getRuntime().exec(command);
    OutputStream stdin = process.getOutputStream();
    InputStream stdout = process.getInputStream();
    
    // Start a background thread that writes input file into "stdin" stream
    ...
    
    // Read the results from "stdout" stream
    ...
    

    见:Unable to read InputStream from Java Process (Runtime.getRuntime().exec() or ProcessBuilder)

    b) 使用 cmd.exe 按原样执行命令

    cmd.exe /c "prog < input > output"
    

    【讨论】:

    • 该命令其实等价于Runtime.getRuntime().exec("prog", new String[] {"&lt;", "input", "&gt;", "output"})
    【解决方案3】:

    您不能对Runtime.exec 使用输入/输出重定向。另一方面,同样的方法返回一个Process对象,你可以访问它的输入和输出流。

    Process process = Runtime.exec("command here");
    
    // these methods are terribly ill-named:
    // getOutputStream returns the process's stdin
    // and getInputStream returns the process's stdout
    OutputStream stdin = process.getOutputStream();
    // write your file in stdin
    stdin.write(...);
    
    // now read from stdout
    InputStream stdout = process.getInputStream();
    stdout.read(...);
    

    【讨论】:

    • 如果您使用的命令像过滤器一样工作,不断地从标准输入读取并同时写入标准输出,这将不起作用。对于大量数据,缓冲区将被耗尽,所有写入都将阻塞。正确的解决方案是使用两个线程或非阻塞 I/O 同时读写。
    • @anttix,这是我希望阅读本文的人知道的那种细节;我只是想指出这些流是存在的。
    【解决方案4】:

    我测试了,没问题。你可以试试。祝你好运

    String cmd = "cmd /c siftWin32 <box.pgm>a.key"; 
    Process process = Runtime.getRuntime().exec(cmd);
    

    【讨论】:

      【解决方案5】:

      *对于通常会导致问题的特殊字符: 此代码即使使用以下文件名也能正常工作:“1 - Volume 1 (Fronte).j​​pg”

      String strArr[] = {"cmd", "/C", file.getCanonicalPath()};
      Process p = rtObj.exec(strArr);///strCmd);
      

      也同意,这里不支持重定向。

      在 Windows 7 上测试 {guscoder:912081574}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        相关资源
        最近更新 更多