ProcessBuilder抛出java.lang.Exception:

时间:2014-03-25 19:57:45

标签: java processbuilder

我想弄清楚为什么下面的代码抛出了

  

java.lang.Exception:没有这样的文件或目录

异常

        ProcessBuilder send = new ProcessBuilder("/bin/bash","/opt/ftp/scripts/XFER.sh | /opt/ftp/myftp -c /opt/ftp/ftp.conf >> /logging/ftp.log2>&1");
        Process sendProcess = send.start();
        br = new BufferedReader(new InputStreamReader(sendProcess.getErrorStream()));
        builder = new StringBuilder();
        line = null;
        while ( (line = br.readLine()) != null) {
           builder.append(line);
           builder.append(System.getProperty("line.separator"));
        }
        if(!builder.toString().isEmpty()){
            throw new Exception( "ERROR with XFER.sh: "+builder.toString() );
        }   

我已尝试隔离String数组中的参数,但这也不起作用。关于什么可能导致这个堆栈跟踪的任何想法?

1 个答案:

答案 0 :(得分:0)

我使用以下代码取得了成功。也许您必须使用-c选项:

private static int execute(String command) {
    Runtime runtime = null;
    Process process = null;

    int exitValue = -1;
    BufferedInputStream bis = null;

    try {
        runtime = Runtime.getRuntime();

        process = runtime.exec(new String[] { "/bin/bash", "-c", command });
        bis = new BufferedInputStream(process.getInputStream());

        byte[] b = new byte[1892];
        while (bis.read(b) != -1) {
        }

        exitValue = process.waitFor();

        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }
        }
        if (process != null) {
            process.destroy();
        }
    } catch (Exception e) {
        //Logging
    }

    return exitValue;
}