2013-04-16 102 views
0

誰能幫我把這個代碼在linuxLinux的find命令不工作

public static void main(String[] args) throws Exception,{ 
    String s ="find . -exec ls -al {} \\;"; 
    Process exec = Runtime 
      .getRuntime() 
      .exec(s); 
    exec.waitFor(); 
System.out.println(s); 
    if (exec.getErrorStream().available() != 0) { 
     BufferedInputStream bf = new BufferedInputStream(
       exec.getErrorStream()); 
     DataInputStream din = new DataInputStream(bf); 
     System.out.println(din.readLine()); 
    } 

    System.out.println(exec.exitValue()); 
} 

我得到以下輸出工作。

find . -exec ls -al {} \; 
find: missing argument to `-exec' 
1 
+0

您正在從錯誤流打印。您可能需要該流程的標準輸出。 –

+0

請看看http://stackoverflow.com/questions/14441652/get-process-output-without-blocking –

+0

目前我有一個退出代碼1.我想退出代碼0(成功)。我正在打印錯誤流,因爲我的退出代碼爲1(失敗)。你們中的任何人都得到了這個代碼工作與退出代碼0,讓我知道。 – user2288031

回答

1

嘗試使用這樣的:

String[] params = {"find", ".", "-exec", "ls", "-al", "{}", ";"}; 

,而不是你的普通字符串命令。

要小心,雖然:輸出很重(我用/ dev /。測試過),waitFor方法可能會導致您的I/O緩衝區突發。

+0

詳細說明這個答案:調用'exec(String)'是不明智的。調用'exec(String [])'是非常可靠的,它不需要反斜槓轉義或引用參數。 – VGR

+0

它工作了,謝謝。 – user2288031

0

太多的反斜槓

find . -exec ls -al {} ';'