2014-03-25 35 views
0

我試圖找出爲什麼下面的代碼被扔的ProcessBuilder扔java.lang.Exception的:

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()); 
     } 

我試過隔離字符串數組中的參數,但那也不管用。任何想法可能會導致這個堆棧跟蹤?

+1

你能編輯你的問題,包括完整的stacktrace,不只是異常類型和消息? –

回答

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; 
} 
+0

這是「-c」。謝謝! – Dan