String[] cmd = new String[] {"sh", "/iso/sha1.sh"};
Process ps = Runtime.getRuntime().exec(cmd);其中sha1.sh文件中的内容为
#!/bin/sh
sha1sum /iso/hello.rar | cut -d ' ' -f1这个可以执行成功,但是我想换一种实现方式,使用如下的方式就不行            String[] cmd = new String[] {"/bin/sh","-c", "sha1sum /iso/hello.rar | cut -d ' ' -f1"};
            Process ps = Runtime.getRuntime().exec(cmd);这段代码就会报IO异常,请问我这个数组参数有问题吗?

解决方案 »

  1.   

    这种编译期的异常需要抛出或者catch
      

  2.   

    现在发现
    String[] cmd = new String[] {"/bin/sh","-c", "sha1sum /iso/hello.rar | cut -d ' ' -f1"};
                Process ps = Runtime.getRuntime().exec(cmd);这段代码有时候能成功,有时候不成功,不成功的时候就抛异常,编译的时候我也捕获异常了,就是IOException,其中上面代码的后面我添加了
    ps.waitFor();,不知道会不会有影响?
      

  3.   

    我不知道你那样写对不对,但我是这样写的:
    String command = new String("gpg --decrypt"+" "+filepath+" >"+rename); 
    String[] cmd= new String[] { "cmd.exe", "/C", command };